ldc-crm-client/src/Models/ArcadiaMailDefault.php
Jérémy Munsch f36447854b
API V3 UPDATE
This removes api authentication with token in favor of signature method.
Refactored code and clean up. It add more unit test plus code coverage
in test/coverage.

- Added   : mardown documentation.
- Changed : model Stats::getQuestionStats  to pass question as route parameter
- Changed : now requests need to have a signature generated with a private key (crm password) using hmac sha256 wrapping url, method and parameters
- Changed : now timestamp will timeout if request is slower than "timestampTimeout", this means crm will reject the request
- Changed : throw ValidationException s instead of TypeErrors
- Changed : timestamp has to be send in request with a DateTime format, it also has to a UTC value
- Fixed   : Change WPCRM dynamic session key, this could prevent spoofing session in some cases.
- Fixed   : closure validation
- Fixed   : model mutator not catching reflectionException
- Fixed   : really send json request using Guzzle internal lib methods.
- Removed : Api AUTH system with api token
- Removed : ApiError::needAuth
- Removed : composer vlucas/dotenv, .env file is not handled any more.
- Removed : LoginException
- Removed : memcached dependency
- Removed : need of memcached
- Removed : unused service provider
- Removed : WpCrm::forceLogin
- Removed : WpCrm::sendArcadiaMailDefault
- Removed : WpCrm::sendArcadiaMailGame
- Updated : PHPCS rules, phpcs fixes and refactored code
2021-12-31 17:00:55 +01:00

408 lines
10 KiB
PHP

<?php
namespace LDC\LDCCRM\Models;
use LDC\LDCCRM\Enums\ApiStatus;
use LDC\LDCCRM\Enums\ArcadiaOrderType;
use LDC\LDCCRM\Utils;
use TypeError;
/**
* This class represents the customer info with
* the account validation status
*
* @property string $email Must be a rfc valid email on a valid domain name
* @property string $firstname The account first name
* @property string $lastname The account last name
* @property string $phone The account phone
* @property string $address The account address (street number, name, build, flat number)
* @property string $address2 The account phone (address complement)
* @property string $city The account city name
* @property string $postcode The account city postcode
*
* @property int $operation The arcadia operation code
* @property int $support_code The arcadia support code
* @property string $label_type_support The arcadia type support label
* @property string $order_date The arcadia order date 'yyyy-mm-dd HH:ii:ss'
* @property int $order_num The arcadia order number
* @property \LDC\LDCCRM\Enums\ArcadiaOrderType $state_order The arcadia order date
* @property int $package_unit_weight The arcadia package unit weight
*
* @category Api Models
* @package LDCCrmClient
* @subpackage models
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
*/
class ArcadiaMailDefault extends CrmClientModel
{
/**
* An RFC Valid Email
*
* Validate: rfc,dns,spoof,filter
*
* Required
* Min length N/A
* Max length 255
* Field Type varchar
*
* @var string
*/
private $email = null;
/**
* First name
*
* Required
* Min length 2
* Max length 255
* Field Type varchar
*
* @var string
*/
private $firstname = null;
/**
* Last name
*
* Required
* Min length 2
* Max length 255
* Field Type varchar
*
* @var string
*/
private $lastname = null;
/**
* The user phone
*
* Validate: sometimes,string
*
* Required
* Min length NA
* Max length 255
* Field Type varchar
*
* @var string
*/
private $phone = null;
/**
* The user address (street number, name, etc)
*
* Validate: sometimes,string
*
* Min length NA
* Max length 255
* Field Type varchar
*
* @var string
*/
private $address = null;
/**
* The user address 2nd part (street number, name, more, etc)
*
* Validate: sometimes,string
*
* Min length NA
* Max length 255
* Field Type varchar
*
* @var string
*/
private $address2 = null;
/**
* The user address city
*
* Validate: sometimes,string
*
* Min length NA
* Max length 255
* Field Type varchar
*
* @var string
*/
private $city = null;
/**
* The user address postcode
*
* Validate: string
*
* Required
* Min length NA
* Max length 255
* Field Type varchar
*
* @var string
*/
private $postcode = null;
/**
* The arcadia operation code
*
* Validate: int
*
* Required
*
* @var integer
*/
private $operation = null;
/**
* The arcadia support code
*
* Validate: int
*
* Required
*
* @var integer
*/
private $support_code = null;
/**
* The arcadia type support label
*
* Validate: string
*
* Required
*
* @var string
*/
private $label_type_support = null;
/**
* The arcadia order date
*
* Validate: date
*
* Required
*
* @var string
*/
private $order_date = null;
/**
* The arcadia order number
*
* Validate: numeric,digits:9
*
* Required
*
* @var integer
*/
private $order_num = null;
/**
* The arcadia state order
*
* Validate: in ArcadiaOrderType::toValues()
*
* Required
*
* @var integer
*/
private $state_order = null;
/**
* The arcadia package unit weight
*
* Validate: integer
*
* Required
*
* @var integer
*/
private $package_unit_weight = null;
/**
* The crm customer fields
*
* @var array
*/
protected $modelFields = [
'email', 'firstname',
'lastname', 'phone', 'address',
'address2', 'city', 'postcode',
'operation', 'support_code', 'label_type_support',
'order_date', 'order_num', 'state_order',
'package_unit_weight',
];
/**
* Validation rules
*
* @var array
*/
protected static $validation = [
'email' => 'required|email',
'firstname' => 'required|string|min:2|max:255',
'lastname' => 'required|string|min:2|max:255',
'phone' => 'sometimes|string|min:10|max:255',
'address' => 'sometimes|string|max:255',
'address2' => 'sometimes|string|max:255',
'city' => 'sometimes|string|max:255',
'postcode' => 'sometimes|string|max:255',
'operation' => 'required|int',
'support_code' => 'required|int',
'label_type_support' => 'required|string|min:2|max:255',
'order_date' => 'required|date_format:Y/m/d H:i:s',
'order_num' => 'required|int',
'state_order' => ['required'],
'package_unit_weight' => 'required|int'
];
/**
* ArcadiaMailDefault
*
* @param array $properties
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a field could not be validated.
*/
public function __construct(array $properties = [])
{
parent::__construct();
$this->fill($properties);
return $this;
}
/**
* Generic accessor
*
* @param string $field
* @return mixed
*/
public function __get(string $field)
{
$props = \array_diff(
$this->modelFields,
$this->invisibleFields
);
if (in_array($field, $props)) {
if ($field === 'state_order' and !\is_object($this->{$field})) {
return ArcadiaOrderType::make($this->{$field});
}
}
return parent::__get($field);
}
/**
* Set prop
*
* @param string $field
* @param mixed $value
* @return void
* @throws \TypeError If the field has an incorrect value.
* @throws \LDC\LDCCRM\Exceptions\AccessError If trying to set a value on a unknown property.
*/
public function __set(string $field, $value): void
{
if ($field === 'state_order') {
$value = (\is_object($value) and
get_class($value) === ArcadiaOrderType::class) ? $value->value : $value;
}
parent::__set($field, $value);
}
/**
* Fill data into this model
* using validation rules
*
* @param array $properties
* @return void
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a field could not be validated.
*/
public function fill(array $properties)
{
if (isset($properties['state_order'])) {
try {
$properties['state_order'] = (\is_object($properties['state_order']) and
get_class($properties['state_order']) === ArcadiaOrderType::class) ?
$properties['state_order']->value : (ctype_digit($properties['state_order']) ?
(int)$properties['state_order'] : $properties['state_order']);
} catch (TypeError | \BadMethodCallException $e) {
// * Cast anyway
$properties['state_order'] = (int)$properties['state_order'];
}
}
parent::fill($properties);
}
/**
* Validate all model properties
*
* @param array $ignore
* @return array the validated values
* @throws ValidationException If a field could not be validated.
*/
protected function validateAllValues(array $ignore = []): array
{
$out = [];
if (!\in_array('state_order', $ignore)) {
try {
$this->validateValue(
'state_order',
ArcadiaOrderType::make(Utils::getRawProperty($this, 'state_order'))
);
} catch (TypeError | \BadMethodCallException $e) {
// Use this to throw validation error.
$this->validateValue('state_order', Utils::getRawProperty($this, 'state_order'));
}
$out['state_order'] = Utils::getRawProperty($this, 'state_order');
}
return array_merge($out, parent::validateAllValues(array_merge(['state_order'], $ignore)));
}
// * PUBLIC METHODS
/**
* Send The actual mail
*
* @return boolean true if succeeded
* @throws ValidationException If a field could not be validated.
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
* Possibles are : - validationFailed, If provided data is wrong(CRM side).
*/
public function send(): bool
{
$this->validateAllValues();
static::setUpApiInterface();
$request = Utils::getResquest('apiV2.arcadia.outgoingMailDefault', [], $this->jsonSerialize());
$response = static::$apiInterface->call($request);
if ($status = $response->status->equals(ApiStatus::OK())) {
$this->updatedValues = [];
}
$this->isNew = !$status;
return $status;
}
/**
* Serialize this object to an array
*
* @return array
*/
public function jsonSerialize()
{
return [
'deliver' => [
'email' => $this->email,
'firstname' => $this->firstname,
'lastname' => $this->lastname,
'phone' => $this->phone,
'address' => $this->address,
'address2' => $this->address2,
'city' => $this->city,
'postcode' => $this->postcode,
],
'operation' => $this->operation,
'support_code' => $this->support_code,
'label_type_support' => $this->label_type_support,
'order_date' => $this->order_date,
'order_num' => $this->order_num,
'state_order' => $this->state_order,
'package_unit_weight' => $this->package_unit_weight
];
}
}