Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CrmApiV4
92.86% covered (success)
92.86%
13 / 14
85.71% covered (warning)
85.71%
6 / 7
8.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 ping
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getCustomerOrNew
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCustomer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setStatus
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unsetStatusWithHashcode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 login
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace LDC\LDCCRM;
4
5use LDC\LDCCRM\Enums\ApiStatus;
6use LDC\LDCCRM\Models\Customer;
7use LDC\LDCCRM\Models\SingleOptin;
8use LDC\LDCCRM\Transport\ApiInterface;
9
10/**
11 * This is the main class to interact with the CRM
12 *
13 * @package LDCCrmClient
14 */
15class CrmApiV4
16{
17    /**
18     * The Api object that will handle requests and responses
19     *
20     * @var \LDC\LDCCRM\Transport\ApiInterface
21     */
22    private static $apiInterface;
23
24    /**
25     * CrmApiV4
26     *
27     * @param string|null $endpoint
28     * @param string|null $bearerToken
29     * @param string|null $bearerPwd
30     */
31    public function __construct(
32        #[\SensitiveParameter] string $endpoint = null,
33        #[\SensitiveParameter] string $bearerToken = null,
34        #[\SensitiveParameter] string $bearerPwd = null
35    ) {
36        self::$apiInterface = self::$apiInterface ?: new ApiInterface(
37            $endpoint,
38            $bearerToken,
39            $bearerPwd
40        );
41        return $this;
42    }
43
44    /**
45     * Use this the ping the crm and know its current status
46     *
47     * @return boolean
48     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs
49     * on server side or on getting response.
50     */
51    public function ping(): bool
52    {
53        $request  = Utils::getResquest('apiV4.ping');
54        $response = self::$apiInterface->call($request);
55        return $response->status->equals(ApiStatus::OK);
56    }
57
58    /**
59     *
60     * Get a customer from its hashcode or email
61     * If it does not exists a new instance
62     * will be returned. You would have
63     * to call its save method later on.
64     *
65     * @param string $identification The customer hashcode or email.
66     * @return \LDC\LDCCRM\Models\Customer A client Customer model.
67     *
68     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
69     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
70     * @phpcs:ignore Squiz.Commenting.FunctionComment.EmptyThrows
71     */
72    public function getCustomerOrNew(#[\SensitiveParameter] string $identification): Customer
73    {
74        return $this->getCustomer($identification) ?? new Customer();
75    }
76
77    /**
78     * Get a customer from its hashcode or email
79     * If it does not exists a new instance
80     * will be returned. You would have
81     * to call its save method later on.
82     *
83     * @param string $identification The customer hashcode or email.
84     * @return \LDC\LDCCRM\Models\Customer|null Return null if the customer was not found
85     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
86     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
87     * Possibles are : - modelNotFound, If this customer hashcode could not be found.
88     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
89     */
90    public function getCustomer(#[\SensitiveParameter] string $identification): ?Customer
91    {
92        return Customer::find($identification);
93    }
94
95    /**
96     * Set the single optin status for an email
97     *
98     * @param string  $email
99     * @param boolean $opt_in
100     * @return boolean true if succeeded
101     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field email could not be validated.
102     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
103     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
104     * Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
105     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
106     */
107    public static function setStatus(#[\SensitiveParameter] string $email, bool $opt_in): bool
108    {
109        return SingleOptin::setStatus($email, $opt_in);
110    }
111
112    /**
113     * Set the single optin status for an email
114     *
115     * @param string $hashcode
116     * @return boolean true if succeeded
117     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field hashcode could not be validated.
118     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
119     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
120     * Possibles are : - modelNotFound, If a single optin does not exists.
121     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
122     */
123    public static function unsetStatusWithHashcode(string $hashcode): bool
124    {
125        return SingleOptin::unsetStatusWithHashcode($hashcode);
126    }
127
128    /**
129     * Test a customer credentials to see if it can login
130     *
131     * @param string $email
132     * @param string $password
133     * @return boolean
134     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
135     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
136     * Possibles are : - authFailed, If credentials are not valid.
137     *                 - customerNotActivated, If the account is not activated.
138     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
139     */
140    public static function login(#[\SensitiveParameter] string $email, #[\SensitiveParameter] string $password): bool
141    {
142        return Customer::login($email, $password);
143    }
144}