diff --git a/composer.json b/composer.json index f634d1e..afb8e33 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,23 @@ }, "autoload": { "psr-4": { - "Mailjet\\LaravelMailjet\\": "src/" + "Mailjet\\LaravelMailjet\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Mailjet\\LaravelMailjet\\Tests\\": "tests" + } + }, + "extra": { + "laravel": { + "providers": [ + "Mailjet\\LaravelMailjet\\MailjetServiceProvider", + "Mailjet\\LaravelMailjet\\MailjetMailServiceProvider" + ], + "aliases": { + "Mailjet": "Mailjet\\LaravelMailjet\\Facades\\Mailjet" + } } }, "config": { diff --git a/src/Contracts/CampaignContract.php b/src/Contracts/CampaignContract.php index 89ac5e7..65eb6b9 100644 --- a/src/Contracts/CampaignContract.php +++ b/src/Contracts/CampaignContract.php @@ -1,17 +1,18 @@ getStatus(); - $message = sprintf('%s: %s', $message, $response->getReasonPhrase()); + $message = "{$message}: {$response->getReasonPhrase()}"; + $this->setErrorFromResponse($response); } + parent::__construct($message, $statusCode, $previous); } /** - * Configure MailjetException from Mailjet\Response - * @method setErrorFromResponse + * Configure MailjetException from Mailjet\Response. + * * @param Response $response */ - private function setErrorFromResponse(Response $response) + private function setErrorFromResponse(Response $response): void { - $this->statusCode = $response->getStatus(); $body = $response->getBody(); + if (isset($body['ErrorInfo'])) { $this->errorInfo = $body['ErrorInfo']; } @@ -58,24 +60,27 @@ private function setErrorFromResponse(Response $response) $this->errorIdentifier = $body['ErrorIdentifier']; } } + /** * @return string */ - public function getErrorInfo() + public function getErrorInfo(): string { return $this->errorInfo; } + /** * @return string */ - public function getErrorMessage() + public function getErrorMessage(): string { return $this->errorMessage; } + /** * @return string */ - public function getErrorIdentifier() + public function getErrorIdentifier(): string { return $this->errorIdentifier; } diff --git a/src/Facades/Mailjet.php b/src/Facades/Mailjet.php index a915abd..f2540e2 100644 --- a/src/Facades/Mailjet.php +++ b/src/Facades/Mailjet.php @@ -1,12 +1,14 @@ app->singleton('Mailjet', function ($app) { - $config = $this->app['config']->get('services.mailjet', array()); + $this->app->singleton('Mailjet', function () { + $config = $this->app['config']->get('services.mailjet', []); $call = $this->app['config']->get('services.mailjet.common.call', true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); + $options = $this->app['config']->get('services.mailjet.common.options', []); return new MailjetService($config['key'], $config['secret'], $call, $options); }); } - - public function provides() + /** + * @return array + */ + public function provides(): array { return ['mailjet']; } diff --git a/src/Model/Campaign.php b/src/Model/Campaign.php index 6f7ddc3..386357b 100644 --- a/src/Model/Campaign.php +++ b/src/Model/Campaign.php @@ -1,90 +1,40 @@ fromEmail = $fromEmail; $this->optionalProperties = $optionalProperties; } /** - * Format Campaign for MailJet API request + * Format Campaign for MailJet API request. + * * @return array */ - public function format() { - - /* * Add the mandatary props* */ - if (!is_null($this->fromEmail)) { - $result[self::FROMEMAIL_KEY] = $this->fromEmail; - } - /* * Add the optional props if any* */ - if (!is_null($this->optionalProperties)) { - $result = array_merge($result, $this->optionalProperties); - } - + public function format(): array + { + $result[self::FROM_EMAIL_KEY] = $this->fromEmail; - return $result; + return array_merge($result, $this->optionalProperties); } - - /** - * Correspond to properties in MailJet request - * Array ['PropertyName' => value, ...] - */ - public function getOptionalProperties() { - return $this->optionalProperties; - } - - /** - * Set array of Campaign properties - * @param array $properties - * @return Properties - */ - public function setOptionalProperties(array $properties) { - $this->optionalProperties = $properties; - return $this->optionalProperties; - } - - /** - * Add a new $property to Campaign - * @param array $property - * @return Properties - */ - public function addOptionalProperty(array $property) { - $this->optionalProperties[] = $property; - return $this->optionalProperties; - } - - /** - * Remove a $property from Campaign - * @param array $property - * @return Properties - */ - public function removeOptionalProperty(array $property) { - foreach (array_keys($this->optionalProperties, $property) as $key) { - unset($this->optionalProperties[$key]); - } - return $this->optionalProperties; - } - } diff --git a/src/Model/CampaignDraft.php b/src/Model/CampaignDraft.php index 659d1a8..bef8dfc 100644 --- a/src/Model/CampaignDraft.php +++ b/src/Model/CampaignDraft.php @@ -1,151 +1,133 @@ locale = $locale; - $this->sender = $sender; - $this->senderEmail = $senderEmail; - $this->subject = $subject; - $this->contactsListID = $contactsListID; - $this->optionalProperties = $optionalProperties; - } + protected $senderEmail; /** - * Format CampaignDraft for MailJet API request - * @return array + * @var string */ - public function format() { - - /* * Add the mandatary props* */ - if (!is_null($this->locale)) { - $result[self::LOCALE_KEY] = $this->locale; - } - - if (!is_null($this->sender)) { - $result[self::SENDER_KEY] = $this->sender; - } - - if (!is_null($this->senderEmail)) { - $result[self::SENDEREMAIL_KEY] = $this->senderEmail; - } - if (!is_null($this->subject)) { - $result[self::SUBJECT_KEY] = $this->subject; - } - if (!is_null($this->contactsListID)) { - $result[self::CONTACTLISTID_KEY] = $this->contactsListID; - } - /* * Add the optional props if any* */ - if (!is_null($this->optionalProperties)) { - $result = array_merge($result, $this->optionalProperties); - } - - - return $result; - } + protected $subject; /** - * Correspond to properties in MailJet request - * Array ['PropertyName' => value, ...] + * @var string */ - public function getOptionalProperties() { - return $this->optionalProperties; - } + protected $contactsListId; /** - * Set array of CampaignDraft properties - * @param array $property - * @return Properties + * @var array|null */ - public function setOptionalProperties(array $properties) { - $this->optionalProperties = $properties; - return $this->optionalProperties; - } + protected $content; /** - * Add a new $property to CampaignDraft - * @param array $property - * @return Properties + * @var string|null */ - public function addOptionalProperty(array $property) { - $this->optionalProperties[] = $property; - return $this->optionalProperties; + protected $id; + + public function __construct(string $locale, + string $sender, + string $senderEmail, + string $subject, + string $contactsListId, + array $optionalProperties = []) + { + $this->locale = $locale; + $this->sender = $sender; + $this->senderEmail = $senderEmail; + $this->subject = $subject; + $this->contactsListId = $contactsListId; + $this->optionalProperties = $optionalProperties; } /** - * Remove a $property from CampaignDraft - * @param array $property - * @return Properties + * Format CampaignDraft for MailJet API request. + * + * @return array */ - public function removeOptionalProperty(array $property) { - foreach (array_keys($this->optionalProperties, $property) as $key) { - unset($this->optionalProperties[$key]); - } - return $this->optionalProperties; + public function format(): array + { + $result = [ + self::LOCALE_KEY => $this->locale, + self::SENDER_KEY => $this->sender, + self::SENDER_EMAIL_KEY => $this->senderEmail, + self::SUBJECT_KEY => $this->subject, + self::CONTACT_LIST_ID_KEY => $this->contactsListId + ]; + + return array_merge($result, $this->optionalProperties); } /** - * Get CampaignDraft content - * @return $content + * Get CampaignDraft content. + * + * @return array|null */ - public function setContent($content) { - $this->content = $content; + public function getContent(): ?array + { + return $this->content; } /** - * Get CampaignDraft content - * @return $content + * Set CampaignDraft content. + * + * @param $content + * + * @return \Mailjet\LaravelMailjet\Model\CampaignDraft */ - public function getContent() { - return $this->content; + public function setContent($content): CampaignDraft + { + $this->content = $content; + + return $this; } /** - * Get CampaignDraft id - * @return $content + * Get CampaignDraft id. + * + * @return string|null */ - public function getId() { + public function getId(): ?string + { return $this->id; } /** - * Set CampaignDraft Id - * @return $content + * Set CampaignDraft id. + * + * @param string $id + * + * @return \Mailjet\LaravelMailjet\Model\CampaignDraft */ - public function setId($id) { + public function setId(string $id): CampaignDraft + { $this->id = $id; - } + return $this; + } } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index cf0543c..32bcb2b 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1,141 +1,149 @@ email = $email; $this->optionalProperties = $optionalProperties; } /** - * Formate contact for MailJet API request + * Format Contact for MailJet API request. + * * @return array */ - public function format() + public function format(): array { $result = [ self::EMAIL_KEY => $this->email, + self::PROPERTIES_KEY => array_filter($this->optionalProperties) ]; - - if (!is_null($this->action)) { - $result[self::ACTION_KEY] = $this->action; - } - if (!is_null($this->optionalProperties)) { - #$result[self::PROPERTIES_KEY] = $this->removeNullProperties($this->properties); - $result[self::PROPERTIES_KEY] = $this->optionalProperties; + if ($this->action !== null) { + $result[self::ACTION_KEY] = $this->action; } return $result; } /** - * Correspond to Email in Mailjet request + * Correspond to Email in Mailjet request. */ - public function getEmail() + public function getEmail(): string { return $this->email; } /** - * Set contact email + * Set contact email. + * * @param string $email + * * @return Contact */ - public function setEmail($email) + public function setEmail($email): Contact { $this->email = $email; + return $this; } /** - * Correspond to Name in MailJet request + * Correspond to Name in MailJet request. */ - public function getName() + public function getName(): ?string { - return $this->optionalProperties[self::NAME_KEY]; + return $this->optionalProperties[self::NAME_KEY] ?? null; } /** - * Set contact name + * Set contact name. + * * @param string $name + * * @return Contact */ - public function setName($name) + public function setName(string $name): Contact { $this->optionalProperties[self::NAME_KEY] = $name; - return $this; - } - - /** - * Correspond to Properties in MailJet request - * Array ['property' => value, ...] - */ - public function getProperties() - { - return $this->optionalProperties; - } - /** - * Set array of Contact properties - * @param array $properties - * @return Contact - */ - public function setProperties(array $properties) - { - $this->optionalProperties = $properties; return $this; } /** - * Action to the contact for Synchronization - * @return string + * Action to the contact for Synchronization. + * + * @return string|null */ - public function getAction() + public function getAction(): ?string { return $this->action; } /** - * Action to the contact for Synchronization - * @param string $action ACTION_* + * Action to the contact for Synchronization. + * + * @param string $action (ACTION_* const) + * * @return Contact */ - public function setAction($action) + public function setAction($action): Contact { + if (! $this->validateAction($action)) { + throw new RuntimeException("$action: is not a valid Action."); + } + $this->action = $action; + return $this; } /** - * Clean null properties to avoid conflict with API - * @param array $properties - * @return array + * Validate action. + * + * @param string $action + * + * @return bool */ - protected function removeNullProperties(array $properties) + protected function validateAction(string $action): bool { - return array_filter($this->optionalProperties); + $available = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; + + return in_array($action, $available); } } diff --git a/src/Model/ContactMetadata.php b/src/Model/ContactMetadata.php index bf51a44..6540f62 100644 --- a/src/Model/ContactMetadata.php +++ b/src/Model/ContactMetadata.php @@ -1,39 +1,72 @@ validateDatatype($datatype)) { + throw new RuntimeException("$datatype: is not a valid Datatype."); + } + $this->name = $name; $this->datatype = $datatype; } /** - * Formate contact for Mailjet API request + * Format contact for Mailjet API request. + * * @return array */ - public function format() + public function format(): array { - $result = [ + return [ 'Name' => $this->name, 'Datatype' => $this->datatype, ]; + } + + /** + * Validate datatype. + * + * @param string $datatype + * + * @return bool + */ + protected function validateDatatype(string $datatype): bool + { + $available = [ + self::DATATYPE_STR, + self::DATATYPE_INT, + self::DATATYPE_FLOAT, + self::DATATYPE_BOOL, + self::DATATYPE_DATETIME, + ]; - return $result; + return in_array($datatype, $available); } } diff --git a/src/Model/ContactsList.php b/src/Model/ContactsList.php index 45035e4..cd95355 100644 --- a/src/Model/ContactsList.php +++ b/src/Model/ContactsList.php @@ -1,32 +1,40 @@ validateAction($action)) { - throw new \RuntimeException("$action: is not a valide Action."); + if (! $this->validateAction($action)) { + throw new RuntimeException("$action: is not a valid Action."); } $this->listId = $listId; @@ -35,22 +43,19 @@ public function __construct($listId, $action, $contacts) } /** - * Formate contactList for MailJet API request + * Format contactList for MailJet API request. + * * @return array */ - public function format() + public function format(): array { $result = [ 'Action' => $this->action, - 'Contacts' => [], ]; - $contacts = $this->contacts; - $contactsArray = array_map(function (Contact $contact) { + $result['Contacts'] = array_map(static function (Contact $contact) { return $contact->format(); - }, $contacts); - - $result['Contacts'] = $contactsArray; + }, $this->contacts); return $result; } @@ -58,19 +63,28 @@ public function format() /** * Get list id */ - public function getListId() + public function getListId(): string { return $this->listId; } /** - * Set Action + * Get action. + */ + public function getAction(): string + { + return $this->action; + } + + /** + * Set action. + * * @param string $action */ - public function setAction($action) + public function setAction($action): ContactsList { - if (!$this->validateAction($action)) { - throw new \RuntimeException("$action: is not a valide Action."); + if (! $this->validateAction($action)) { + throw new RuntimeException("$action: is not a valid Action."); } $this->action = $action; @@ -79,32 +93,24 @@ public function setAction($action) } /** - * Get action + * Get contacts. */ - public function getAction() - { - return $this->action; - } - - /** - * Get contacts - */ - public function getContacts() + public function getContacts(): array { return $this->contacts; } /** - * Validate if action is authorized + * Validate action name. + * * @param string $action + * * @return bool */ - private function validateAction($action) + protected function validateAction($action): bool { - $actionAvailable = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; - if (in_array($action, $actionAvailable)) { - return true; - } - return false; + $actionsAvailable = [self::ACTION_ADDFORCE, self::ACTION_ADDNOFORCE, self::ACTION_REMOVE, self::ACTION_UNSUB]; + + return in_array($action, $actionsAvailable); } } diff --git a/src/Model/EventCallbackUrl.php b/src/Model/EventCallbackUrl.php index 1f92dd6..a863973 100644 --- a/src/Model/EventCallbackUrl.php +++ b/src/Model/EventCallbackUrl.php @@ -1,85 +1,147 @@ validateType($type)) { + throw new RuntimeException("$type: is not a valid event type."); + } + + if (! $this->validateStatus($status)) { + throw new RuntimeException("$status: is not a valid event status."); + } + $this->url = $url; - $this->eventType = $eventType; + $this->type = $type; $this->isBackup = $isBackup; $this->status = $status; $this->version = $version; - $this->apikeyId = $apikeyId; + $this->apiKeyId = $apiKeyId; $this->groupEvent = $groupEvent; } /** - * Formate contactList for MailJet API request + * Format contactList for MailJet API request. + * * @return array */ - public function format() + public function format(): array { if ($this->groupEvent) { - // ugly fix for misunderstanding of this... + // Events are grouped only in API version 2. $this->version = 2; } $result = [ 'Url' => $this->url, - 'EventType' => $this->eventType, - 'IsBackup' => $this->isBackup, - 'Status' => $this->status, - 'Version' => $this->version + 'EventType' => $this->type, + 'IsBackup' => $this->isBackup, + 'Status' => $this->status, + 'Version' => $this->version, ]; - if($this->apikeyId){ - $result['APIKeyID'] = $this->apikeyId; + if ($this->apiKeyId) { + $result['APIKeyID'] = $this->apiKeyId; } return $result; } + + /** + * Validate event type. + * + * @param string $type + * + * @return bool + */ + protected function validateType(string $type): bool + { + $available = [ + self::EVENT_TYPE_OPEN, + self::EVENT_TYPE_CLICK, + self::EVENT_TYPE_BOUNCE, + self::EVENT_TYPE_SPAM, + self::EVENT_TYPE_BLOCKED, + self::EVENT_TYPE_UNSUB, + self::EVENT_TYPE_SENT, + ]; + + return in_array($type, $available); + } + + /** + * Validate event status. + * + * @param string $status + * + * @return bool + */ + protected function validateStatus(string $status): bool + { + $available = [self::EVENT_STATUS_ALIVE, self::EVENT_STATUS_DEAD]; + + return in_array($status, $available); + } } diff --git a/src/Model/Model.php b/src/Model/Model.php new file mode 100644 index 0000000..f3e040a --- /dev/null +++ b/src/Model/Model.php @@ -0,0 +1,75 @@ + value, ...] + * + * @return array + */ + public function getOptionalProperties(): array + { + return $this->optionalProperties; + } + + /** + * Set array of optional properties. + * + * @param array $properties + * + * @return array + */ + public function setOptionalProperties(array $properties): array + { + $this->optionalProperties = $properties; + + return $this->optionalProperties; + } + + /** + * Add a new optional property. + * + * @param array $property + * + * @return array + */ + public function addOptionalProperty(array $property): array + { + $this->optionalProperties[] = $property; + + return $this->optionalProperties; + } + + /** + * Remove a optional property. + * + * @param array $property + * + * @return array + */ + public function removeOptionalProperty(array $property): array + { + foreach (array_keys($this->optionalProperties, $property) as $key) { + unset($this->optionalProperties[$key]); + } + + return $this->optionalProperties; + } +} diff --git a/src/Model/Requestable.php b/src/Model/Requestable.php new file mode 100644 index 0000000..4e384c6 --- /dev/null +++ b/src/Model/Requestable.php @@ -0,0 +1,15 @@ +name = $name; - $this->optionalProperties = $optionalProperties; - } + protected $name; /** - * Format Template for MailJet API request - * @return array + * @var array|null */ - public function format() { - - /* * Add the mandatary props* */ - if (!is_null($this->name)) { - $result[self::NAME_KEY] = $this->name; - } - - /* * Add the optional props if any* */ - if (!is_null($this->optionalProperties)) { - $result = array_merge($result, $this->optionalProperties); - } - - - return $result; - } + protected $content; /** - * Correspond to properties in MailJet request - * Array ['PropertyName' => value, ...] + * @var string|null */ - public function getOptionalProperties() { - return $this->optionalProperties; - } + protected $id; - /** - * Set array of Template properties - * @param array $property - * @return Properties - */ - public function setOptionalProperties(array $properties) { - $this->optionalProperties = $properties; - return $this->optionalProperties; + public function __construct(string $name, array $optionalProperties = []) + { + $this->name = $name; + $this->optionalProperties = $optionalProperties; } /** - * Add a new $property to Template - * @param array $property - * @return Properties + * Format Template for MailJet API request. + * + * @return array */ - public function addProperty(array $property) { - $this->optionalProperties[] = $property; - return $this->optionalProperties; - } + public function format(): array + { + $result[self::NAME_KEY] = $this->name; - /** - * Remove a $property from Template - * @param array $property - * @return Properties - */ - public function removeProperty(array $property) { - foreach (array_keys($this->optionalProperties, $property) as $key) { - unset($this->optionalProperties[$key]); - } - return $this->optionalProperties; + return array_merge($result, $this->optionalProperties); } /** * Get Template content - * @return $content + * + * @return array|null $content */ - public function setContent($content) { - $this->content = $content; + public function getContent(): ?array + { + return $this->content; } /** - * Get Template content - * @return $content + * Set Template content. + * + * @param array $content + * + * @return \Mailjet\LaravelMailjet\Model\Template */ - public function getContent() { - return $this->content; + public function setContent(array $content): Template + { + $this->content = $content; + + return $this; } /** - * Get Id - * @return $content + * Get id. + * + * @return string|null */ - public function getId() { + public function getId(): ?string + { return $this->id; } /** - * Set Id + * Set id. + * + * @param string $id + * + * @return \Mailjet\LaravelMailjet\Model\Template */ - public function setId($id) { + public function setId(string $id): Template + { $this->id = $id; - } + return $this; + } } diff --git a/src/Providers/CampaignDraftServiceProvider.php b/src/Providers/CampaignDraftServiceProvider.php index 81eec0e..d0de3dc 100644 --- a/src/Providers/CampaignDraftServiceProvider.php +++ b/src/Providers/CampaignDraftServiceProvider.php @@ -1,13 +1,21 @@ app->bind('Mailjet\LaravelMailjet\Contracts\CampaignDraftContract', - function($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.common.call',true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); - $mailjetService=new MailjetService($config['key'], $config['secret'], $call,$options); + $this->app->bind(CampaignDraftContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.common.call', true); + $options = $this->app['config']->get('services.mailjet.common.options', []); + + $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); + return new CampaignDraftService($mailjetService); }); } @@ -42,8 +50,8 @@ function($app) { * * @return array */ - public function provides() + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\CampaignDraftContract']; + return [CampaignDraftContract::class]; } -} \ No newline at end of file +} diff --git a/src/Providers/CampaignServiceProvider.php b/src/Providers/CampaignServiceProvider.php index 0631998..150cf6c 100644 --- a/src/Providers/CampaignServiceProvider.php +++ b/src/Providers/CampaignServiceProvider.php @@ -1,13 +1,21 @@ app->bind('Mailjet\LaravelMailjet\Contracts\CampaignContract', - function($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.common.call',true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); - $mailjetService=new MailjetService($config['key'], $config['secret'], $call,$options); + $this->app->bind(CampaignContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.common.call', true); + $options = $this->app['config']->get('services.mailjet.common.options', []); + + $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); + return new CampaignService($mailjetService); }); } @@ -42,8 +50,8 @@ function($app) { * * @return array */ - public function provides() + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\CampaignContract']; + return [CampaignContract::class]; } -} \ No newline at end of file +} diff --git a/src/Providers/ContactMetadataServiceProvider.php b/src/Providers/ContactMetadataServiceProvider.php index 10bce5b..38d3cff 100644 --- a/src/Providers/ContactMetadataServiceProvider.php +++ b/src/Providers/ContactMetadataServiceProvider.php @@ -1,13 +1,21 @@ app->bind('Mailjet\LaravelMailjet\Contracts\ContactMetadataContract', - function($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.common.call',true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); - $mailjetService=new MailjetService($config['key'], $config['secret'], $call,$options); + $this->app->bind(ContactMetadataContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.common.call', true); + $options = $this->app['config']->get('services.mailjet.common.options', []); + + $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); + return new ContactMetadataService($mailjetService); }); } @@ -42,8 +50,8 @@ function($app) { * * @return array */ - public function provides() + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\ContactMetadataContract']; + return [ContactMetadataContract::class]; } -} \ No newline at end of file +} diff --git a/src/Providers/ContactsListServiceProvider.php b/src/Providers/ContactsListServiceProvider.php index 9a08f84..14a8917 100644 --- a/src/Providers/ContactsListServiceProvider.php +++ b/src/Providers/ContactsListServiceProvider.php @@ -1,13 +1,21 @@ app->bind('Mailjet\LaravelMailjet\Contracts\ContactsListContract', - function($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.common.call',true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); - $mailjetService=new MailjetService($config['key'], $config['secret'], $call,$options); + $this->app->bind(ContactsListContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.common.call', true); + $options = $this->app['config']->get('services.mailjet.common.options', []); + + $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); + return new ContactsListService($mailjetService); }); } @@ -42,8 +50,8 @@ function($app) { * * @return array */ - public function provides() + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\ContactsListContract']; + return [ContactsListContract::class]; } -} \ No newline at end of file +} diff --git a/src/Providers/EventCallbackUrlServiceProvider.php b/src/Providers/EventCallbackUrlServiceProvider.php index ab689ec..a7f6d67 100644 --- a/src/Providers/EventCallbackUrlServiceProvider.php +++ b/src/Providers/EventCallbackUrlServiceProvider.php @@ -1,13 +1,21 @@ app->bind('Mailjet\LaravelMailjet\Contracts\EventCallbackUrlContract', - function($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.common.call',true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); - $mailjetService=new MailjetService($config['key'], $config['secret'], $call,$options); + $this->app->bind(EventCallbackUrlContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.common.call', true); + $options = $this->app['config']->get('services.mailjet.common.options', []); + + $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); + return new EventCallbackUrlService($mailjetService); }); } @@ -42,8 +50,8 @@ function($app) { * * @return array */ - public function provides() + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\EventCallbackUrlContract']; + return [EventCallbackUrlContract::class]; } -} \ No newline at end of file +} diff --git a/src/Providers/MailjetClientServiceProvider.php b/src/Providers/MailjetClientServiceProvider.php index 0049a8a..e7eb098 100644 --- a/src/Providers/MailjetClientServiceProvider.php +++ b/src/Providers/MailjetClientServiceProvider.php @@ -1,19 +1,28 @@ app->bind('Mailjet\LaravelMailjet\Contracts\MailjetServiceContract', function ($app) { - $config = $this->app['config']->get('services.mailjet', array()); + $this->app->bind(MailjetServiceContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); $call = $this->app['config']->get('services.mailjet.common.call', true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); + $options = $this->app['config']->get('services.mailjet.common.options', []); return new MailjetService($config['key'], $config['secret'], $call, $options); }); } - - public function provides() + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\MailjetServiceContract']; + return [MailjetServiceContract::class]; } } diff --git a/src/Providers/TemplateServiceProvider.php b/src/Providers/TemplateServiceProvider.php index fbd464c..8080536 100644 --- a/src/Providers/TemplateServiceProvider.php +++ b/src/Providers/TemplateServiceProvider.php @@ -1,13 +1,21 @@ app->bind('Mailjet\LaravelMailjet\Contracts\TemplateServiceContract', - function($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.common.call',true); - $options = $this->app['config']->get('services.mailjet.common.options', array()); - $mailjetService=new MailjetService($config['key'], $config['secret'], $call,$options); + $this->app->bind(TemplateServiceContract::class, function () { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.common.call', true); + $options = $this->app['config']->get('services.mailjet.common.options', []); + + $mailjetService = new MailjetService($config['key'], $config['secret'], $call, $options); + return new TemplateService($mailjetService); }); } @@ -42,8 +50,8 @@ function($app) { * * @return array */ - public function provides() + public function provides(): array { - return ['Mailjet\LaravelMailjet\Contracts\TemplateServiceContract']; + return [TemplateServiceContract::class]; } -} \ No newline at end of file +} diff --git a/src/Services/CampaignDraftService.php b/src/Services/CampaignDraftService.php index 21d9eb9..bfa613d 100644 --- a/src/Services/CampaignDraftService.php +++ b/src/Services/CampaignDraftService.php @@ -1,23 +1,22 @@ mailjet->get(Resources::$Campaigndraft, - ['filters' => $filters]); - if (!$response->success()) { - $this->throwError("CampaignDraftService :getAllCampaignDrafts() failed", - $response); + $response = $this->mailjet->get(Resources::$Campaigndraft, ['filters' => $filters]); + + if (! $response->success()) { + throw new MailjetException(0, 'CampaignDraftService :getAllCampaignDrafts() failed', $response); } return $response->getData(); } /** - * Access a given campaigndraft resource - * @param string $CampaignId + * Access a given CampaignDraft resource. + * + * @param string $id + * * @return array */ - public function findByCampaignDraftId($CampaignId) + public function findByCampaignDraftId(string $id) { $response = $this->mailjet->get(Resources::$Campaigndraft, - ['id' => $CampaignId]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:findByCampaignDraftId() failed", + ['id' => $id]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:findByCampaignDraftId() failed', $response); } @@ -61,14 +63,15 @@ public function findByCampaignDraftId($CampaignId) /** * create a new fresh CampaignDraft + * * @param Campaigndraft $campaignDraft */ public function create(CampaignDraft $campaignDraft) { $response = $this->mailjet->post(Resources::$Campaigndraft, ['body' => $campaignDraft->format()]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:create() failed", $response); + if (! $response->success()) { + $this->throwError('CampaignDraftService:create() failed', $response); } return $response->getData(); @@ -76,15 +79,16 @@ public function create(CampaignDraft $campaignDraft) /** * Update one specific campaigndraft resource - * @param int $id + * + * @param int $id * @param Campaigndraft $campaignDraft */ - public function update($CampaignId, CampaignDraft $campaignDraft) + public function update($id, CampaignDraft $campaignDraft) { $response = $this->mailjet->put(Resources::$Campaigndraft, - ['id' => $CampaignId, 'body' => $campaignDraft->format()]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:update() failed", $response); + ['id' => $id, 'body' => $campaignDraft->format()]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:update() failed', $response); } return $response->getData(); @@ -92,15 +96,17 @@ public function update($CampaignId, CampaignDraft $campaignDraft) /** * Return the text and html contents of the campaigndraft + * * @param string $id + * * @return array */ - public function getDetailContent($id) + public function getDetailContent(string $id) { $response = $this->mailjet->get(Resources::$CampaigndraftDetailcontent, ['id' => $id]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:getDetailContent failed", + if (! $response->success()) { + $this->throwError('CampaignDraftService:getDetailContent failed', $response); } @@ -109,15 +115,17 @@ public function getDetailContent($id) /** * Creates the content of a campaigndraft + * * @param string $id + * * @return array */ public function createDetailContent($id, $contentData) { $response = $this->mailjet->post(Resources::$CampaigndraftDetailcontent, ['id' => $id, 'body' => $contentData]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:createDetailContent failed", + if (! $response->success()) { + $this->throwError('CampaignDraftService:createDetailContent failed', $response); } @@ -126,15 +134,17 @@ public function createDetailContent($id, $contentData) /** * Return the date of the scheduled sending of the campaigndraft + * * @param string Campaign $id + * * @return array */ - public function getSchedule($CampaignId) + public function getSchedule(string $id) { $response = $this->mailjet->get(Resources::$CampaigndraftSchedule, - ['id' => $CampaignId]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:getSchedule failed", + ['id' => $id]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:getSchedule failed', $response); } @@ -143,16 +153,18 @@ public function getSchedule($CampaignId) /** * Schedule when the campaigndraft will be sent - * @param string Campaign $id - * @param RFC3339 format "Y-m-d\TH:i:sP" $date + * + * @param string $id + * @param string $date (RFC3339 format "Y-m-d\TH:i:sP") + * * @return array */ - public function scheduleCampaign($CampaignId, $date) + public function scheduleCampaign(string $id, string $date) { $response = $this->mailjet->post(Resources::$CampaigndraftSchedule, - ['id' => $CampaignId, 'body' => $date]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:scheduleCampaign failed", + ['id' => $id, 'body' => $date]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:scheduleCampaign failed', $response); } @@ -161,16 +173,18 @@ public function scheduleCampaign($CampaignId, $date) /** * Update the date when the campaigndraft will be sent + * * @param string Campaign $id * @param string Schedule $date + * * @return array */ - public function updateCampaignSchedule($CampaignId, $date) + public function updateCampaignSchedule($id, $date) { $response = $this->mailjet->put(Resources::$CampaigndraftSchedule, - ['id' => $CampaignId, 'body' => $date]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:updateCampaignSchedule failed", + ['id' => $id, 'body' => $date]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:updateCampaignSchedule failed', $response); } @@ -179,15 +193,17 @@ public function updateCampaignSchedule($CampaignId, $date) /** * Cancel a future sending + * * @param string Campaign $id + * * @return array */ - public function removeSchedule($CampaignId) + public function removeSchedule(string $id) { $response = $this->mailjet->delete(Resources::$CampaigndraftSchedule, - ['id' => $CampaignId]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:removeSchedule failed", + ['id' => $id]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:removeSchedule failed', $response); } @@ -196,15 +212,17 @@ public function removeSchedule($CampaignId) /** * Send the campaign immediately + * * @param string Campaign $id + * * @return array */ - public function sendCampaign($CampaignId) + public function sendCampaign(string $id) { $response = $this->mailjet->post(Resources::$CampaigndraftSend, - ['id' => $CampaignId]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:sendCampaign failed", + ['id' => $id]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:sendCampaign failed', $response); } @@ -213,15 +231,17 @@ public function sendCampaign($CampaignId) /** * Return the status of a CampaignDraft + * * @param string Campaign $id + * * @return array */ - public function getCampaignStatus($CampaignId) + public function getCampaignStatus(string $id) { $response = $this->mailjet->get(Resources::$CampaigndraftStatus, - ['id' => $CampaignId]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:getCampaignStatus failed", + ['id' => $id]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:getCampaignStatus failed', $response); } @@ -230,16 +250,18 @@ public function getCampaignStatus($CampaignId) /** * An action to test a CampaignDraft. + * * @param string Campaign $id * @param array of $recipients + * * @return array */ - public function testCampaign($CampaignId, $recipients) + public function testCampaign($id, $recipients) { $response = $this->mailjet->post(Resources::$CampaigndraftTest, - ['id' => $CampaignId, 'body' => $recipients]); - if (!$response->success()) { - $this->throwError("CampaignDraftService:testCampaign failed", + ['id' => $id, 'body' => $recipients]); + if (! $response->success()) { + $this->throwError('CampaignDraftService:testCampaign failed', $response); } @@ -250,4 +272,4 @@ private function throwError($title, Response $response) { throw new MailjetException(0, $title, $response); } -} \ No newline at end of file +} diff --git a/src/Services/CampaignService.php b/src/Services/CampaignService.php index d41a72c..c246b3c 100644 --- a/src/Services/CampaignService.php +++ b/src/Services/CampaignService.php @@ -1,87 +1,100 @@ mailjet = $mailjet; } /** * List campaigns resources available for this apikey + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getAllCampaigns(array $filters = null) { + public function getAllCampaigns(array $filters = null): array + { $response = $this->mailjet->get(Resources::$Campaign, ['filters' => $filters]); - if (!$response->success()) { - $this->throwError("CampaignService:getAllCampaigns() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'CampaignService:getAllCampaigns() failed', $response); } return $response->getData(); } /** - * Access a given campaign resource - * @param string $CampaignId + * Access a given campaign resource. + * + * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function findByCampaignId($CampaignId) { - $response = $this->mailjet->get(Resources::$Campaign, ['id' => $CampaignId]); - if (!$response->success()) { - $this->throwError("CampaignService:findByCampaignId() failed", $response); + public function findByCampaignId(string $id): array + { + $response = $this->mailjet->get(Resources::$Campaign, ['id' => $id]); + + if (! $response->success()) { + throw new MailjetException(0, 'CampaignService:findByCampaignId() failed', $response); } return $response->getData(); } - /** - * Find a given campaign by Newsletter/campaigndraftId - * @param string $NewsletterId + /** + * Find a given campaign by Newsletter / CampaignDraft id. + * + * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function findByNewsletterId($NewsletterId) { - $response = $this->mailjet->get(Resources::$Campaign, ['mj.nl' => $NewsletterId]); - if (!$response->success()) { - $this->throwError("CampaignService:findByNewsletterId() failed", $response); + public function findByNewsletterId(string $id): array + { + $response = $this->mailjet->get(Resources::$Campaign, ['mj.nl' => $id]); + + if (! $response->success()) { + throw new MailjetException(0, 'CampaignService:findByNewsletterId() failed', $response); } return $response->getData(); } /** - * Update one specific campaign resource with a PUT request, providing the campaign's ID value + * Update one specific campaign resource with a PUT request. + * * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function updateCampaign($CampaignId, Campaign $campaign) { - $response = $this->mailjet->put(Resources::$Campaign, ['id' => $CampaignId, 'body' => $campaign->format()]); - if (!$response->success()) { - $this->throwError("CampaignService:updateCampaign() failed", $response); + public function updateCampaign(string $id, Campaign $campaign): array + { + $response = $this->mailjet->put(Resources::$Campaign, ['id' => $id, 'body' => $campaign->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'CampaignService:updateCampaign() failed', $response); } return $response->getData(); } - - private function throwError($title, Response $response) { - throw new MailjetException(0, $title, $response); - } - } diff --git a/src/Services/ContactMetadataService.php b/src/Services/ContactMetadataService.php index 6fa5403..42415c3 100644 --- a/src/Services/ContactMetadataService.php +++ b/src/Services/ContactMetadataService.php @@ -1,59 +1,60 @@ mailjet = $mailjet; } /** - * Retrieve all ContactMetadata + * Retrieve all ContactMetadata. + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getAll() + public function getAll(): array { $response = $this->mailjet->get(Resources::$Contactmetadata); - if (!$response->success()) { - $this->throwError("ContactMetadataService:getAll() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactMetadataService:getAll() failed', $response); } return $response->getData(); } /** - * Retrieve one ContactMetadata + * Retrieve one ContactMetadata. + * * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function get($id) + public function get(string $id): array { $response = $this->mailjet->get(Resources::$Contactmetadata, ['id' => $id]); - if (!$response->success()) { - $this->throwError("ContactMetadataService:get() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactMetadataService:get() failed', $response); } return $response->getData(); @@ -61,13 +62,17 @@ public function get($id) /** * create a new fresh ContactMetadata - * @param ContactMetadata $contactMetadata + * + * @param ContactMetadata $metadata + * + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function create(ContactMetadata $contactMetadata) + public function create(ContactMetadata $metadata): array { - $response = $this->mailjet->post(Resources::$Contactmetadata, ['body' => $contactMetadata->format()]); - if (!$response->success()) { - $this->throwError("ContactMetadataService:create() failed", $response); + $response = $this->mailjet->post(Resources::$Contactmetadata, ['body' => $metadata->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactMetadataService:create() failed', $response); } return $response->getData(); @@ -75,14 +80,18 @@ public function create(ContactMetadata $contactMetadata) /** * Update one ContactMetadata - * @param int $id - * @param ContactMetadata $contactMetadata + * + * @param string $id + * @param ContactMetadata $metadata + * + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function update($id, ContactMetadata $contactMetadata) + public function update(string $id, ContactMetadata $metadata): array { - $response = $this->mailjet->put(Resources::$Contactmetadata, ['id' => $id,'body' => $contactMetadata->format()]); - if (!$response->success()) { - $this->throwError("ContactMetadataService:update() failed", $response); + $response = $this->mailjet->put(Resources::$Contactmetadata, ['id' => $id, 'body' => $metadata->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactMetadataService:update() failed', $response); } return $response->getData(); @@ -90,25 +99,19 @@ public function update($id, ContactMetadata $contactMetadata) /** * Delete one ContactMetadata - * @param int $id + * + * @param string $id + * + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function delete($id) + public function delete(string $id): array { $response = $this->mailjet->delete(Resources::$Contactmetadata, ['id' => $id]); - if (!$response->success()) { - $this->throwError("ContactMetadataService:delete() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactMetadataService:delete() failed', $response); } return $response->getData(); } - - /** - * Helper to throw error - * @param string $title - * @param Response $response - */ - private function throwError($title, Response $response) - { - throw new MailjetException(0, $title, $response); - } } diff --git a/src/Services/ContactsListService.php b/src/Services/ContactsListService.php index 5b5b748..f101297 100644 --- a/src/Services/ContactsListService.php +++ b/src/Services/ContactsListService.php @@ -1,107 +1,122 @@ mailjet = $mailjet; } /** - * create a new fresh Contact to listId - * @param string $listId + * Create a new fresh Contact to listId. + * + * @param string $id * @param Contact $contact - * @param string $action + * @param string $action + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function create($listId, Contact $contact, $action=Contact::ACTION_ADDFORCE) + public function create(string $id, Contact $contact, $action = Contact::ACTION_ADDFORCE): array { $contact->setAction($action); - $response = $this->_exec($listId, $contact); - if (!$response->success()) { - $this->throwError("ContactsListService:create() failed", $response); + + $response = $this->_exec($id, $contact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:create() failed', $response); } return $response->getData(); } /** - * update a Contact to listId - * @param string $listId + * Update a Contact to listId. + * + * @param string $id * @param Contact $contact - * @param string $action + * @param string $action + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function update($listId, Contact $contact, $action=Contact::ACTION_ADDNOFORCE) + public function update(string $id, Contact $contact, $action = Contact::ACTION_ADDNOFORCE): array { $contact->setAction($action); - $response = $this->_exec($listId, $contact); - if (!$response->success()) { - $this->throwError("ContactsListService:update() failed", $response); + + $response = $this->_exec($id, $contact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:update() failed', $response); } return $response->getData(); } /** - * re/subscribe a Contact to listId - * @param string $listId + * Re/subscribe a Contact to listId. + * + * @param string $id * @param Contact $contact - * @param bool $force + * @param bool $force + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function subscribe($listId, Contact $contact, $force = true) + public function subscribe(string $id, Contact $contact, bool $force = true): array { - if ($force) { - $contact->setAction(Contact::ACTION_ADDFORCE); - } else { - $contact->setAction(Contact::ACTION_ADDNOFORCE); - } - $response = $this->_exec($listId, $contact); - if (!$response->success()) { - $this->throwError("ContactsListService:subscribe() failed", $response); + $contact->setAction($force ? Contact::ACTION_ADDFORCE : Contact::ACTION_ADDNOFORCE); + + $response = $this->_exec($id, $contact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:subscribe() failed', $response); } return $response->getData(); } /** - * unsubscribe a Contact from listId - * @param string $listId + * Unsubscribe a Contact from listId. + * + * @param string $id * @param Contact $contact + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function unsubscribe($listId, Contact $contact) + public function unsubscribe(string $id, Contact $contact): array { $contact->setAction(Contact::ACTION_UNSUB); - $response = $this->_exec($listId, $contact); - if (!$response->success()) { - $this->throwError("ContactsListService:unsubscribe() failed", $response); + + $response = $this->_exec($id, $contact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:unsubscribe() failed', $response); } return $response->getData(); @@ -109,111 +124,118 @@ public function unsubscribe($listId, Contact $contact) /** * Delete a Contact from listId - * @param string $listId + * + * @param string $id * @param Contact $contact + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function delete($listId, Contact $contact) + public function delete(string $id, Contact $contact): array { $contact->setAction(Contact::ACTION_REMOVE); - $response = $this->_exec($listId, $contact); - if (!$response->success()) { - $this->throwError("ContactsListService:delete() failed", $response); + + $response = $this->_exec($id, $contact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:delete() failed', $response); } return $response->getData(); } /** - * Change email a Contact - * @param string $listId + * Change email a Contact. + * + * @param string $id * @param Contact $contact - * @param string $oldEmail + * @param string $oldEmail + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function updateEmail($listId, Contact $contact, $oldEmail) + public function updateEmail(string $id, Contact $contact, string $oldEmail): array { - // get old contact properties $response = $this->mailjet->get(Resources::$Contactdata, ['id' => $oldEmail]); - if (!$response->success()) { - $this->throwError("ContactsListService:changeEmail() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); } - // copy contact properties $oldContactData = $response->getData(); + if (isset($oldContactData[0])) { $contact->setProperties($oldContactData[0]['Data']); } - // add new contact $contact->setAction(Contact::ACTION_ADDFORCE); - $response = $this->_exec($listId, $contact); - if (!$response->success()) { - $this->throwError("ContactsListService:changeEmail() failed", $response); + $response = $this->_exec($id, $contact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); } - // remove old $oldContact = new Contact($oldEmail); $oldContact->setAction(Contact::ACTION_REMOVE); - $response = $this->_exec($listId, $oldContact); - if (!$response->success()) { - $this->throwError("ContactsListService:changeEmail() failed", $response); + $response = $this->_exec($id, $oldContact); + + if (! $response->success()) { + throw new MailjetException(0, 'ContactsListService:changeEmail() failed', $response); } return $response->getData(); } /** - * Import many contacts to a list + * Import many contacts to a list. * https://dev.mailjet.com/email-api/v3/contactslist-managemanycontacts/ - * @param ContactsList $contactsList + * + * @param ContactsList $list + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function uploadManyContactsList(ContactsList $contactsList) + public function uploadManyContactsList(ContactsList $list): array { $batchResults = []; - // we send multiple smaller requests instead of a bigger one - $contactChunks = array_chunk($contactsList->getContacts(), self::CONTACT_BATCH_SIZE); + $contactChunks = array_chunk($list->getContacts(), self::CONTACT_BATCH_SIZE); + foreach ($contactChunks as $contactChunk) { - // create a sub-contactList to divide large request - $subContactsList = new ContactsList($contactsList->getListId(), $contactsList->getAction(), $contactChunk); + $subContactsList = new ContactsList($list->getListId(), $list->getAction(), $contactChunk); $currentBatch = $this->mailjet->post(Resources::$ContactslistManagemanycontacts, ['id' => $subContactsList->getListId(), 'body' => $subContactsList->format()] ); + if ($currentBatch->success()) { - array_push($batchResults, $currentBatch->getData()[0]); + $batchResults[] = $currentBatch->getData()[0]; } else { - $this->throwError("ContactsListService:manageManyContactsList() failed", $currentBatch); + throw new MailjetException(0, 'ContactsListService:manageManyContactsList() failed', $currentBatch); } } + return $batchResults; } /** - * An action for adding a contact to a contact list. Only POST is supported. - * The API will internally create the new contact if it does not exist, - * add or update the name and properties. - * The properties have to be defined before they can be used. - * The API then adds the contact to the contact list with active=true and - * unsub=specified value if it is not already in the list, - * or updates the entry with these values. On success, - * the API returns a packet with the same format but with all properties available - * for that contact. - * @param string $listId - * @param Contact $contact - */ - private function _exec($listId, Contact $contact) + * An action for adding a contact to a contact list. Only POST is supported. + * The API will internally create the new contact if it does not exist, + * add or update the name and properties. + * The properties have to be defined before they can be used. + * The API then adds the contact to the contact list with active=true + * and unsub=specified value if it is not already in the list, + * or updates the entry with these values. + * On success, the API returns a packet with the same format + * but with all properties available for that contact. + * + * @param string $id + * @param Contact $contact + * + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException + */ + private function _exec(string $id, Contact $contact): Response { return $this->mailjet->post(Resources::$ContactslistManagecontact, - ['id' => $listId, 'body' => $contact->format()] + ['id' => $id, 'body' => $contact->format()] ); } - - /** - * Helper to throw error - * @param string $title - * @param Response $response - */ - private function throwError($title, Response $response) - { - throw new MailjetException(0, $title, $response); - } } diff --git a/src/Services/EventCallbackUrlService.php b/src/Services/EventCallbackUrlService.php index 8859ed5..b03c76b 100644 --- a/src/Services/EventCallbackUrlService.php +++ b/src/Services/EventCallbackUrlService.php @@ -1,118 +1,120 @@ mailjet = $mailjet; } /** - * Retrieve all EventCallbackUrl + * Retrieve all EventCallbackUrl. + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getAll() + public function getAll(): array { $response = $this->mailjet->get(Resources::$Eventcallbackurl); - if (!$response->success()) { - $this->throwError("EventCallbackUrlService:getAll() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'EventCallbackUrlService:getAll() failed', $response); } return $response->getData(); } /** - * Retrieve one EventCallbackUrl + * Retrieve one EventCallbackUrl. + * * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function get($id) + public function get(string $id): array { $response = $this->mailjet->get(Resources::$Eventcallbackurl, ['id' => $id]); - if (!$response->success()) { - $this->throwError("EventCallbackUrlService:get() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'EventCallbackUrlService:get() failed', $response); } return $response->getData(); } /** - * Create one EventCallbackUrl - * @param EventCallbackUrl $eventCallbackUrl + * Create one EventCallbackUrl. + * + * @param EventCallbackUrl $url + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function create(EventCallbackUrl $eventCallbackUrl) + public function create(EventCallbackUrl $url): array { - $response = $this->mailjet->post(Resources::$Eventcallbackurl, ['body' => $eventCallbackUrl->format()]); - if (!$response->success()) { - $this->throwError("EventCallbackUrlService:create() failed", $response); + $response = $this->mailjet->post(Resources::$Eventcallbackurl, ['body' => $url->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'EventCallbackUrlService:create() failed', $response); } return $response->getData(); } /** - * Update one EventCallbackUrl - * @param string $id - * @param EventCallbackUrl $eventCallbackUrl + * Update one EventCallbackUrl. + * + * @param string $id + * @param EventCallbackUrl $url + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function update($id, EventCallbackUrl $eventCallbackUrl) + public function update(string $id, EventCallbackUrl $url): array { - $response = $this->mailjet->put(Resources::$Eventcallbackurl, ['id' => $id, 'body' => $eventCallbackUrl->format()]); - if (!$response->success()) { - $this->throwError("EventCallbackUrlService:update() failed", $response); + $response = $this->mailjet->put(Resources::$Eventcallbackurl, ['id' => $id, 'body' => $url->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'EventCallbackUrlService:update() failed', $response); } return $response->getData(); } /** - * Delete one EventCallbackUrl + * Delete one EventCallbackUrl. + * * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function delete($id) + public function delete(string $id): array { $response = $this->mailjet->delete(Resources::$Eventcallbackurl, ['id' => $id]); - if (!$response->success()) { - $this->throwError("EventCallbackUrlService:delete() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'EventCallbackUrlService:delete() failed', $response); } return $response->getData(); } - - /** - * Helper to throw error - * @param string $title - * @param Response $response - * @param array $response - */ - private function throwError($title, Response $response) - { - throw new MailjetException(0, $title, $response); - } -} \ No newline at end of file +} diff --git a/src/Services/MailjetService.php b/src/Services/MailjetService.php index 8634655..1420ebb 100644 --- a/src/Services/MailjetService.php +++ b/src/Services/MailjetService.php @@ -1,219 +1,253 @@ client = new Client($key, $secret, $call, $settings); } /** - * Trigger a POST request + * Trigger a POST request. * * @param array $resource Mailjet Resource/Action pair * @param array $args Request arguments * @param array $options * * @return Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function post($resource, array $args = [], array $options = []) + public function post(array $resource, array $args = [], array $options = []): Response { $response = $this->client->post($resource, $args, $options); - if (!$response->success()) { - $this->throwError("MailjetService:post() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:post() failed', $response); } + return $response; } /** - * Trigger a GET request + * Trigger a GET request. * * @param array $resource Mailjet Resource/Action pair * @param array $args Request arguments * @param array $options * * @return Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function get($resource, array $args = [], array $options = []) + public function get(array $resource, array $args = [], array $options = []): Response { $response = $this->client->get($resource, $args, $options); - if (!$response->success()) { - $this->throwError("MailjetService:get() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:get() failed', $response); } + return $response; } /** - * Trigger a PUT request + * Trigger a PUT request. * * @param array $resource Mailjet Resource/Action pair * @param array $args Request arguments * @param array $options * * @return Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function put($resource, array $args = [], array $options = []) + public function put(array $resource, array $args = [], array $options = []): Response { $response = $this->client->put($resource, $args, $options); - if (!$response->success()) { - $this->throwError("MailjetService:put() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:put() failed', $response); } + return $response; } /** - * Trigger a DELETE request + * Trigger a DELETE request. * * @param array $resource Mailjet Resource/Action pair * @param array $args Request arguments * @param array $options * * @return Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function delete($resource, array $args = [], array $options = []) + public function delete(array $resource, array $args = [], array $options = []): Response { $response = $this->client->delete($resource, $args, $options); - if (!$response->success()) { - $this->throwError("MailjetService:delete() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:delete() failed', $response); } + return $response; } - - - /**TODO exclude HIGH Level API methods into managers**/ /** - * Get all list on your mailjet account - * @param array $filters Filters that will be use to filter the request. See mailjet API documentation for all filters available - * @return array + * Get all list on your Mailjet account. + * TODO: Exclude HIGH Level API methods into managers. + * + * @param array $filters Filters that will be use to filter the request + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getAllLists($filters) + public function getAllLists(array $filters = null): Response { $response = $this->client->get(Resources::$Contactslist, ['filters' => $filters]); - if (!$response->success()) { - $this->throwError("MailjetService:getAllLists() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:getAllLists() failed', $response); } + return $response; } /** - * Create a new list - * @param array $body array containing the list informations. the 'Name' parameter is mandatory.See mailjet API documentation for all parameters available - * @return array + * Create a new list. + * + * @param array $body Information list - the 'Name' field is mandatory. + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function createList($body) + public function createList(array $body): Response { $response = $this->client->post(Resources::$Contactslist, ['body' => $body]); - if (!$response->success()) { - $this->throwError("MailjetService:createList() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:createList() failed', $response); } + return $response; } /** - * Get all list recipient on your mailjet account - * @param array $filters Filters that will be use to filter the request. See mailjet API documentation for all filters available - * @return array + * Get all list recipient on your Mailjet account. + * + * @param array $filters Filters that will be use to filter the request. + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getListRecipients($filters) + public function getListRecipients(array $filters = null): Response { $response = $this->client->get(Resources::$Listrecipient, ['filters' => $filters]); - if (!$response->success()) { - $this->throwError("MailjetService:getListRecipients() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:getListRecipients() failed', $response); } + return $response; } /** - * Get single contact informations. - * @param int $id ID of the contact - * @return array + * Get single contact information. + * + * @param string $id + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getSingleContact($id) + public function getSingleContact(string $id): Response { $response = $this->client->get(Resources::$Contact, ['id' => $id]); - if (!$response->success()) { - $this->throwError("MailjetService:getSingleContact() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:getSingleContact() failed', $response); } + return $response; } /** - * create a contact - * @param array $body array containing the list informations. the 'Email' parameter is mandatory.See mailjet API documentation for all parameters available - * @return array + * Create a contact. + * + * @param array $body Information list - the 'Email' field is mandatory. + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function createContact($body) + public function createContact(array $body): Response { $response = $this->client->post(Resources::$Contact, ['body' => $body]); - if (!$response->success()) { - $this->throwError("MailjetService:createContact() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:createContact() failed', $response); } + return $response; } /** - * create a listrecipient (relationship between contact and list) - * @param array $body array containing the list informations. the 'ContactID' and 'ListID' parameters are mandatory.See mailjet API documentation for all parameters available - * @return array + * Create a list recipient (relationship between contact and list). + * + * @param array $body Information list - the 'ContactID' and 'ListID' parameters are mandatory. + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function createListRecipient($body) + public function createListRecipient(array $body): Response { $response = $this->client->post(Resources::$Listrecipient, ['body' => $body]); - if (!$response->success()) { - $this->throwError("MailjetService:createListRecipient() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:createListRecipient() failed', $response); } + return $response; } /** - * edit a list recipient - * @param int $id id of the list recipient - * @param array $body array containing the list informations. the 'ContactID' and 'ListID' parameters are mandatory.See mailjet API documentation for all parameters available - * @return array + * Edit a list recipient. + * + * @param string $id + * @param array $body Information list - the 'ContactID' and 'ListID' parameters are mandatory. + * + * @return \Mailjet\Response + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function editListrecipient($id, $body) + public function editListRecipient(string $id, array $body): Response { - $response = $this->client->put(Resources::$Listrecipient, ['id'=>$id, 'body' => $body]); - if (!$response->success()) { - $this->throwError("MailjetService:editListrecipient() failed", $response); + $response = $this->client->put(Resources::$Listrecipient, ['id' => $id, 'body' => $body]); + + if (! $response->success()) { + throw new MailjetException(0, 'MailjetService:editListrecipient() failed', $response); } + return $response; } /** - * Retrieve Mailjet\Client - * @return Client + * Retrieve Mailjet client. + * + * @return \Mailjet\Client */ - public function getClient() + public function getClient(): Client { return $this->client; } - - /** - * Helper to throw error - * @param string $title - * @param Response $response - */ - private function throwError($title, Response $response) - { - throw new MailjetException(0, $title, $response); - } } diff --git a/src/Services/TemplateService.php b/src/Services/TemplateService.php index b44bc96..183142a 100644 --- a/src/Services/TemplateService.php +++ b/src/Services/TemplateService.php @@ -1,23 +1,21 @@ mailjet->get(Resources::$Template, - ['filters' => $filters]); - if (!$response->success()) { - $this->throwError("TemplateService :getAll() failed", $response); + $response = $this->mailjet->get(Resources::$Template, ['filters' => $filters]); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:getAll() failed', $response); } return $response->getData(); } /** - * Access a given template resource, use a GET request, providing the template's ID value + * Access a given template resource, use a GET request, providing the template's ID value. + * * @param string $id - * @return $Template + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function get($id) + public function get(string $id): array { $response = $this->mailjet->get(Resources::$Template, ['id' => $id]); - if (!$response->success()) { - $this->throwError("TemplateService:get() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:get() failed', $response); } return $response->getData(); @@ -60,102 +65,117 @@ public function get($id) /** * Add a new template resource with a POST request. - * @param Template $Template + * + * @param Template $template + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function create(Template $Template) + public function create(Template $template): array { - $response = $this->mailjet->post(Resources::$Template, - ['body' => $Template->format()]); - if (!$response->success()) { - $this->throwError("TemplateService:create() failed", $response); + $response = $this->mailjet->post(Resources::$Template, ['body' => $template->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:create() failed', $response); } return $response->getData(); } /** - * Update one specific template resource with a PUT request, providing the template's ID value - * @param int $id - * @param Template $Template + * Update one specific template resource with a PUT request, providing the template's ID value. + * + * @param string $id + * @param Template $template + * + * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function update($id, Template $Template) + public function update(string $id, Template $template): array { - $response = $this->mailjet->put(Resources::$Template, - ['id' => $id, 'body' => $Template->format()]); - if (!$response->success()) { - $this->throwError("TemplateService:update() failed", $response); + $response = $this->mailjet->put(Resources::$Template, ['id' => $id, 'body' => $template->format()]); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:update() failed', $response); } return $response->getData(); } /** - * delete a given template + * Delete a given template. + * * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function delete($id) + public function delete(string $id): array { $response = $this->mailjet->delete(Resources::$Template, ['id' => $id]); - if (!$response->success()) { - $this->throwError("TemplateService:delete() failed", $response); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:delete() failed', $response); } return $response->getData(); } /** - * Return the text and html contents of the Template + * Return the text and html contents of the Template. + * * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function getDetailContent($id) + public function getDetailContent(string $id): array { - $response = $this->mailjet->get(Resources::$TemplateDetailcontent, - ['id' => $id]); - if (!$response->success()) { - $this->throwError("TemplateService:getDetailContent failed", - $response); + $response = $this->mailjet->get(Resources::$TemplateDetailcontent, ['id' => $id]); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:getDetailContent failed', $response); } return $response->getData(); } /** - * Creates the content of a Template + * Creates the content of a Template. + * + * @param string $id + * @param array $content + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function createDetailContent($id, $contentData) + public function createDetailContent(string $id, array $content): array { - $response = $this->mailjet->post(Resources::$TemplateDetailcontent, - ['id' => $id, 'body' => $contentData]); - if (!$response->success()) { - $this->throwError("TemplateService:createDetailContent failed", - $response); + $response = $this->mailjet->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => $content]); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:createDetailContent failed', $response); } return $response->getData(); } /** - * Deletes the content of a Template + * Deletes the content of a Template. + * + * @param string $id + * * @return array + * @throws \Mailjet\LaravelMailjet\Exception\MailjetException */ - public function deleteDetailContent($id) + public function deleteDetailContent(string $id): array { - $nullContent = null; - $response = $this->mailjet->post(Resources::$TemplateDetailcontent, - ['id' => $id, 'body' => $nullContent]); - if (!$response->success()) { - $this->throwError("TemplateService:createDetailContent failed", - $response); + $response = $this->mailjet->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => null]); + + if (! $response->success()) { + throw new MailjetException(0, 'TemplateService:createDetailContent failed', $response); } return $response->getData(); } - - private function throwError($title, Response $response) - { - throw new MailjetException(0, $title, $response); - } -} \ No newline at end of file +} diff --git a/src/Transport/MailjetTransport.php b/src/Transport/MailjetTransport.php index a99bd22..9c21ec4 100644 --- a/src/Transport/MailjetTransport.php +++ b/src/Transport/MailjetTransport.php @@ -1,5 +1,7 @@ setUpMocks(); - $this->service_provider = new CampaignDraftServiceProvider($this->application_mock); + $this->serviceProvider = new CampaignDraftServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\CampaignDraftContract', $this->service_provider->provides()); + $this->assertContains(CampaignDraftContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Providers/CampaignServiceProviderTest.php b/tests/Providers/CampaignServiceProviderTest.php index 2042759..b219f3d 100644 --- a/tests/Providers/CampaignServiceProviderTest.php +++ b/tests/Providers/CampaignServiceProviderTest.php @@ -1,62 +1,64 @@ setUpMocks(); - $this->service_provider = new CampaignServiceProvider($this->application_mock); + $this->serviceProvider = new CampaignServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\CampaignContract', $this->service_provider->provides()); + $this->assertContains(CampaignContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Providers/ContactMetadataServiceProviderTest.php b/tests/Providers/ContactMetadataServiceProviderTest.php index 43077f7..789551c 100644 --- a/tests/Providers/ContactMetadataServiceProviderTest.php +++ b/tests/Providers/ContactMetadataServiceProviderTest.php @@ -1,62 +1,64 @@ setUpMocks(); - $this->service_provider = new ContactMetadataServiceProvider($this->application_mock); + $this->serviceProvider = new ContactMetadataServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\ContactMetadataContract', $this->service_provider->provides()); + $this->assertContains(ContactMetadataContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Providers/ContactsListServiceProviderTest.php b/tests/Providers/ContactsListServiceProviderTest.php index d6d7ee8..fb9436d 100644 --- a/tests/Providers/ContactsListServiceProviderTest.php +++ b/tests/Providers/ContactsListServiceProviderTest.php @@ -1,62 +1,64 @@ setUpMocks(); - $this->service_provider = new ContactsListServiceProvider($this->application_mock); + $this->serviceProvider = new ContactsListServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\ContactsListContract', $this->service_provider->provides()); + $this->assertContains(ContactsListContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Providers/EventCallbackUrlServiceProviderTest.php b/tests/Providers/EventCallbackUrlServiceProviderTest.php index eb6eb7d..eb2ebdd 100644 --- a/tests/Providers/EventCallbackUrlServiceProviderTest.php +++ b/tests/Providers/EventCallbackUrlServiceProviderTest.php @@ -1,62 +1,64 @@ setUpMocks(); - $this->service_provider = new EventCallbackUrlServiceProvider($this->application_mock); + $this->serviceProvider = new EventCallbackUrlServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\EventCallbackUrlContract', $this->service_provider->provides()); + $this->assertContains(EventCallbackUrlContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Providers/MailjetClientServiceProviderTest.php b/tests/Providers/MailjetClientServiceProviderTest.php index 5f35a31..ea8b1f8 100644 --- a/tests/Providers/MailjetClientServiceProviderTest.php +++ b/tests/Providers/MailjetClientServiceProviderTest.php @@ -1,62 +1,64 @@ setUpMocks(); - $this->service_provider = new MailjetClientServiceProvider($this->application_mock); + $this->serviceProvider = new MailjetClientServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\MailjetServiceContract', $this->service_provider->provides()); + $this->assertContains(MailjetServiceContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Providers/TemplateServiceProviderTest.php b/tests/Providers/TemplateServiceProviderTest.php index b1a8356..00a669d 100644 --- a/tests/Providers/TemplateServiceProviderTest.php +++ b/tests/Providers/TemplateServiceProviderTest.php @@ -1,62 +1,64 @@ setUpMocks(); - $this->service_provider = new TemplateServiceProvider($this->application_mock); + $this->serviceProvider = new TemplateServiceProvider($this->application); parent::setUp(); } - protected function setUpMocks() - { - $this->application_mock = Mockery::mock(Application::class); - $this->application_mock->shouldReceive('bind'); - } - /** * @test */ - public function it_can_be_constructed() + public function it_can_be_constructed(): void { - $this->assertInstanceOf(ServiceProvider::class, $this->service_provider); + $this->assertInstanceOf(ServiceProvider::class, $this->serviceProvider); } /** * @test */ - public function it_does_provide_method() + public function it_does_provide_method(): void { - $this->assertContains('Mailjet\LaravelMailjet\Contracts\TemplateServiceContract', $this->service_provider->provides()); + $this->assertContains(TemplateServiceContract::class, $this->serviceProvider->provides()); } /** * @test */ - public function it_performs_nothing_in_a_boot_method() + public function it_performs_nothing_in_a_boot_method(): void + { + $this->assertNull($this->serviceProvider->boot()); + } + + protected function setUpMocks(): void { - $this->assertNull($this->service_provider->boot()); - + $this->application = Mockery::mock(Application::class); + $this->application->shouldReceive('bind'); } -} \ No newline at end of file +} diff --git a/tests/Services/MailjetServiceTest.php b/tests/Services/MailjetServiceTest.php index 4cff008..ef48078 100644 --- a/tests/Services/MailjetServiceTest.php +++ b/tests/Services/MailjetServiceTest.php @@ -1,13 +1,16 @@ andReturnUsing(function($resourceArg, $params) use ($responseMock) { $data = []; - switch ($resourceArg) { + switch ($resourceArg[0]) { case 'resource-test-put': $data = array_merge($params, [ 'status' => '0003', ]); break; - case Resources::$Listrecipient: + case 'listrecipient': $data = array_merge($params, [ 'status' => '0011', ]); @@ -59,26 +62,26 @@ public function setUp(): void ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { $data = []; - switch ($resourceArg) { + switch ($resourceArg[0]) { case 'resource-test-post': $data = array_merge($params, [ 'status' => '0001', ]); break; - case Resources::$Contactslist: + case 'contactslist': $data = array_merge($params, [ 'status' => '0006', ]); break; - case Resources::$Contact: + case 'contact': $data = array_merge($params, [ 'status' => '0009', ]); break; - case Resources::$Listrecipient: + case 'listrecipient': $data = array_merge($params, [ 'status' => '0010', ]); @@ -96,7 +99,7 @@ public function setUp(): void ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { $data = []; - switch ($resourceArg) { + switch ($resourceArg[0]) { case 'resource-test-delete': $data = array_merge($params, [ 'status' => '0004', @@ -114,26 +117,26 @@ public function setUp(): void ->andReturnUsing(function($resourceArg, $params) use ($responseMock) { $data = []; - switch ($resourceArg) { + switch ($resourceArg[0]) { case 'resource-test-get': $data = array_merge($params, [ 'status' => '0002', ]); break; - case Resources::$Contactslist: + case 'contactslist': $data = array_merge($params, [ 'status' => '0005', ]); break; - case Resources::$Listrecipient: + case 'listrecipient': $data = array_merge($params, [ 'status' => '0007', ]); break; - case Resources::$Contact: + case 'contact': $data = array_merge($params, [ 'status' => '0008', ]); @@ -148,7 +151,7 @@ public function setUp(): void $this->mailjetService = $this->app['Mailjet']; } - public function testFacade() + public function testFacade(): void { $this->assertTrue(method_exists($this->mailjetService, 'get')); $this->assertTrue(method_exists($this->mailjetService, 'post')); @@ -164,15 +167,15 @@ public function testFacade() $this->assertTrue(method_exists($this->mailjetService, 'getClient')); } - public function testCanUseClient() + public function testCanUseClient(): void { - $client = \Mailjet::getClient(); - $this->assertEquals("Mailjet\Client", get_class($client)); + $client = Mailjet::getClient(); + $this->assertInstanceOf(Client::class, $client); } public function testPost() { - $response = $this->mailjetService->post('resource-test-post', [ + $response = $this->mailjetService->post(['resource-test-post'], [ 'data' => 'test0001', ]); @@ -184,7 +187,7 @@ public function testPost() public function testGet() { - $response = $this->mailjetService->get('resource-test-get', [ + $response = $this->mailjetService->get(['resource-test-get'], [ 'data' => 'test0002', ], []); @@ -196,7 +199,7 @@ public function testGet() public function testPut() { - $response = $this->mailjetService->put('resource-test-put', [ + $response = $this->mailjetService->put(['resource-test-put'], [ 'data' => 'test0003', ], []); @@ -208,7 +211,7 @@ public function testPut() public function testDelete() { - $response = $this->mailjetService->delete('resource-test-delete', [ + $response = $this->mailjetService->delete(['resource-test-delete'], [ 'data' => 'test0004', ], []); @@ -262,10 +265,10 @@ public function testGetListRecipients() public function testGetSingleContact() { - $response = $this->mailjetService->getSingleContact(123); + $response = $this->mailjetService->getSingleContact('123'); $this->assertSame([ - 'id' => 123, + 'id' => '123', 'status' => '0008', ], $response->getData()); } @@ -300,12 +303,12 @@ public function testCreateListRecipient() public function testEditListrecipient() { - $response = $this->mailjetService->editListrecipient(123, [ + $response = $this->mailjetService->editListrecipient('1233', [ 'data' => 'test0011' ]); $this->assertSame([ - 'id' => 123, + 'id' => '1233', 'body' => [ 'data' => 'test0011', ], @@ -313,10 +316,10 @@ public function testEditListrecipient() ], $response->getData()); } - protected function getPackageAliases($app) + protected function getPackageAliases($app): array { return [ - 'Mailjet' => \Mailjet\LaravelMailjet\Facades\Mailjet::class + 'Mailjet' => Mailjet::class ]; } @@ -326,15 +329,15 @@ protected function getPackageAliases($app) * @param \Illuminate\Foundation\Application $app * @return void */ - protected function getEnvironmentSetUp($app) + protected function getEnvironmentSetUp($app): void { // Setup default database to use sqlite :memory: $app['config']->set('services.mailjet.key', 'ABC123456'); $app['config']->set('services.mailjet.secret', 'ABC123456'); } - protected function getPackageProviders($app) + protected function getPackageProviders($app): array { - return ['\Mailjet\LaravelMailjet\MailjetServiceProvider']; + return [MailjetServiceProvider::class]; } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 65b7b04..5fb6079 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,2 +1,5 @@