CustomerRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 36
c 1
b 0
f 0
dl 0
loc 149
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneByEmailAndWebsiteId() 0 12 1
A loadCustomerByWebsiteIdAndIncrementId() 0 12 1
A init() 0 14 1
A findDirectoryCountryRegions() 0 17 2
A load() 0 6 1
A findAll() 0 5 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Repositories\CustomerRepository
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2018 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-customer
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Customer\Repositories;
16
17
use TechDivision\Import\Customer\Utils\MemberNames;
18
use TechDivision\Import\Customer\Utils\SqlStatementKeys;
19
use TechDivision\Import\Dbal\Collection\Repositories\AbstractRepository;
20
21
/**
22
 * Repository implementation to load customer data.
23
 *
24
 * @author    Tim Wagner <[email protected]>
25
 * @copyright 2018 TechDivision GmbH <[email protected]>
26
 * @license   https://opensource.org/licenses/MIT
27
 * @link      https://github.com/techdivision/import-customer
28
 * @link      http://www.techdivision.com
29
 */
30
class CustomerRepository extends AbstractRepository implements CustomerRepositoryInterface
31
{
32
33
    /**
34
     * The prepared statement to load a customer with the passed entity ID.
35
     *
36
     * @var \PDOStatement
37
     */
38
    protected $customerStmt;
39
40
    /**
41
     * The prepared statement to load a customer with the passed email and website ID.
42
     *
43
     * @var \PDOStatement
44
     */
45
    protected $customerByEmailAndWebsiteIdStmt;
46
47
    /**
48
     * The prepared statement to load a customer with the passed email and website ID.
49
     *
50
     * @var \PDOStatement
51
     */
52
    protected $customerByWebsiteIdAndIncrementIdStmt;
53
54
    /**
55
     * The prepared statement to load the existing customers.
56
     *
57
     * @var \PDOStatement
58
     */
59
    protected $customersStmt;
60
61
    /**
62
     * @var PDOStatement
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Cust...positories\PDOStatement was not found. Did you mean PDOStatement? If so, make sure to prefix the type with \.
Loading history...
63
     */
64
    protected $directoryCountryRegionsStatement;
65
    /**
66
     * Initializes the repository's prepared statements.
67
     *
68
     * @return void
69
     */
70
    public function init()
71
    {
72
73
        // initialize the prepared statements
74
        $this->customerStmt =
75
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMER));
76
        $this->customerByEmailAndWebsiteIdStmt =
77
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMER_BY_EMAIL_AND_WEBSITE_ID));
78
        $this->customerByWebsiteIdAndIncrementIdStmt =
79
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMER_BY_WEBSITE_ID_AND_INCREMET_ID));
80
        $this->customersStmt =
81
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::CUSTOMERS));
82
        $this->directoryCountryRegionsStatement =
83
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::SELECT_DIRECTORY_COUNTRY_REGIONS));
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getConnection()->...CTORY_COUNTRY_REGIONS)) of type PDOStatement is incompatible with the declared type TechDivision\Import\Cust...positories\PDOStatement of property $directoryCountryRegionsStatement.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
    }
85
86
    /**
87
     * Return's the available customers.
88
     *
89
     * @return array The available customers
90
     */
91
    public function findAll()
92
    {
93
        // load and return the available customers
94
        $this->customersStmt->execute();
95
        return $this->customersStmt->fetchAll(\PDO::FETCH_ASSOC);
96
    }
97
98
    /**
99
     * Return's the customer with the passed entity ID.
100
     *
101
     * @param integer $id The entity ID of the customer to return
102
     *
103
     * @return array|null The customer
104
     */
105
    public function load($id)
106
    {
107
108
        // if not, try to load the customer with the passed entity ID
109
        $this->customerStmt->execute(array(MemberNames::ENTITY_ID => $id));
110
        return $this->customerStmt->fetch(\PDO::FETCH_ASSOC);
111
    }
112
113
    /**
114
     * Return's the customer with the passed email and website ID.
115
     *
116
     * @param string $email     The email of the customer to return
117
     * @param string $websiteId The website ID of the customer to return
118
     *
119
     * @return array|null The customer
120
     */
121
    public function findOneByEmailAndWebsiteId($email, $websiteId)
122
    {
123
124
        // initialize the params
125
        $params = array(
126
            MemberNames::EMAIL      => $email,
127
            MemberNames::WEBSITE_ID => $websiteId
128
        );
129
130
        // if not, try to load the customer with the passed email and website ID
131
        $this->customerByEmailAndWebsiteIdStmt->execute($params);
132
        return $this->customerByEmailAndWebsiteIdStmt->fetch(\PDO::FETCH_ASSOC);
133
    }
134
135
    /**
136
     * Return's the customer with the passed email, website ID and increment id.
137
     *
138
     * @param string $websiteId    The website ID of the customer to return
139
     * @param string $increment_id The website ID of the customer to return
140
     *
141
     * @return array|null The customer
142
     */
143
    public function loadCustomerByWebsiteIdAndIncrementId($websiteId, $increment_id)
144
    {
145
146
        // initialize the params
147
        $params = array(
148
            MemberNames::WEBSITE_ID => $websiteId,
149
            MemberNames::INCREMENT_ID => $increment_id
150
        );
151
152
        // if not, try to load the customer with the passed email and website ID
153
        $this->customerByWebsiteIdAndIncrementIdStmt->execute($params);
154
        return $this->customerByWebsiteIdAndIncrementIdStmt->fetch(\PDO::FETCH_ASSOC);
155
    }
156
157
    /**
158
     * Return's all country regions from directory
159
     *
160
     * @return array
161
     */
162
    public function findDirectoryCountryRegions()
163
    {
164
        $directoryCountryRegions = [];
165
166
        // execute the prepared statement
167
        $this->directoryCountryRegionsStatement->execute();
168
169
        // load the available customer groups
170
        $avalaibleDirectoryCountryRegions = $this->directoryCountryRegionsStatement->fetchAll();
171
172
        // fetch the directory regions and assemble them as array with the codes as key
173
        foreach ($avalaibleDirectoryCountryRegions as $countryRegion) {
174
            $directoryCountryRegions[$countryRegion[\TechDivision\Import\Customer\Address\Utils\MemberNames::DIRECTORY_REGION_CODE]] = $countryRegion;
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Cust...dress\Utils\MemberNames was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
175
        }
176
177
        // return the customer groups
178
        return $directoryCountryRegions;
179
    }
180
}
181