Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SingleOptin
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 setStatus
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 unsetStatusWithHashcode
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace LDC\LDCCRM\Models;
4
5use LDC\LDCCRM\Enums\ApiStatus;
6use LDC\LDCCRM\Utils;
7
8/**
9 * This class allows to handle single opt in
10 *
11 * @category Api Models
12 * @package LDCCrmClient
13 * @subpackage models
14 * @author Jérémy Munsch <jeremy.munsch@ldc.fr>
15 */
16abstract class SingleOptin extends ClientModel
17{
18    /**
19     * Validation rules
20     *
21     * @var array
22     */
23    protected static array $validation = [
24        'email'    => 'required|email',
25        'hashcode' => 'required|string|size:36',
26        'opt_in'   => 'required|bool',
27    ];
28
29    /**
30     * Set the single opt in status for an email
31     *
32     * @param string  $email
33     * @param boolean $opt_in
34     * @return boolean true if succeeded
35     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field email could not be validated.
36     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
37     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
38     * Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
39     */
40    public static function setStatus(#[\SensitiveParameter] string $email, bool $opt_in): bool
41    {
42        self::validateValue('email', $email);
43        static::setUpApiInterface();
44        $request = Utils::getResquest('apiV4.optin', [], [
45            'email' => $email,
46            'opt_in' => $opt_in
47        ]);
48
49        $response = static::$apiInterface->call($request);
50        return $response->status->equals(ApiStatus::OK);
51    }
52
53    /**
54     * Unset the single opt in for a hashcode (unsubscribe)
55     *
56     * @param string $hashcode
57     * @return boolean true if succeeded
58     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field hashcode could not be validated.
59     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
60     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
61     * Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
62     */
63    public static function unsetStatusWithHashcode(string $hashcode): bool
64    {
65        self::validateValue('hashcode', $hashcode);
66        static::setUpApiInterface();
67        $request = Utils::getResquest('apiV4.optout-hashcode', [], [
68            'hashcode' => $hashcode
69        ]);
70
71        $response = static::$apiInterface->call($request);
72        return $response->status->equals(ApiStatus::OK);
73    }
74}