Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
10 / 10
CrmApiV2
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
10 / 10
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 ping
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getCustomerOrNew
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCustomer
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setStatus
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 login
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
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 CrmApiV2
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     * CrmApiV2
26     *
27     * @param string|null $endpoint
28     * @param string|null $bearerToken
29     * @param string|null $bearerPwd
30     */
31    public function __construct(string $endpoint = null, string $bearerToken = null, string $bearerPwd = null)
32    {
33        self::$apiInterface = self::$apiInterface ?: new ApiInterface(
34            $endpoint,
35            $bearerToken,
36            $bearerPwd
37        );
38        return $this;
39    }
40
41    /**
42     * Use this the ping the crm and know its current status
43     *
44     * @return boolean
45     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs
46     * on server side or on getting response.
47     */
48    public function ping(): bool
49    {
50        $request  = Utils::getResquest('apiV2.ping');
51        $response = self::$apiInterface->call($request);
52        return $response->status->equals(ApiStatus::OK());
53    }
54
55    /**
56     *
57     * Get a customer from its hashcode or email
58     * If it does not exists a new instance
59     * will be returned. You would have
60     * to call its save method later on.
61     *
62     * @param string $identification The customer hashcode or email.
63     * @return \LDC\LDCCRM\Models\Customer A client Customer model.
64     *
65     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
66     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
67     * @phpcs:ignore Squiz.Commenting.FunctionComment.EmptyThrows
68     */
69    public function getCustomerOrNew(string $identification): Customer
70    {
71        return $this->getCustomer($identification) ?? new Customer();
72    }
73
74    /**
75     * Get a customer from its hashcode or email
76     * If it does not exists a new instance
77     * will be returned. You would have
78     * to call its save method later on.
79     *
80     * @param string $identification The customer hashcode or email.
81     * @return \LDC\LDCCRM\Models\Customer|null Return null if the customer was not found
82     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
83     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
84     * Possibles are : - modelNotFound, If this customer hashcode could not be found.
85     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
86     */
87    public function getCustomer(string $identification): ?Customer
88    {
89        return Customer::find($identification);
90    }
91
92    /**
93     * Set the single optin status for an email
94     *
95     * @param string  $email
96     * @param boolean $opt_in
97     * @return boolean true if succeeded
98     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
99     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
100     * Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
101     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
102     */
103    public static function setStatus(string $email, bool $opt_in): bool
104    {
105        return SingleOptin::setStatus($email, $opt_in);
106    }
107
108    /**
109     * Test a customer credentials to see if it can login
110     *
111     * @param string $email
112     * @param string $password
113     * @return boolean
114     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
115     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
116     * Possibles are : - authFailed, If credentials are not valid.
117     *                 - customerNotActivated, If the account is not activated.
118     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
119     */
120    public static function login(string $email, string $password): bool
121    {
122        return Customer::login($email, $password);
123    }
124}