Completed
Push — master ( 3a08fe...aee93b )
by Tim
18:05 queued 16:13
created

UrlRewriteRepository::getEntityName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\UrlRewriteRepository
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Repositories;
22
23
use TechDivision\Import\Utils\CacheKeys;
24
use TechDivision\Import\Utils\MemberNames;
25
use TechDivision\Import\Utils\SqlStatementKeys;
26
use TechDivision\Import\Dbal\Collection\Repositories\AbstractFinderRepository;
27
28
/**
29
 * Repository implementation to load URL rewrite data.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import
35
 * @link      http://www.techdivision.com
36
 */
37
class UrlRewriteRepository extends AbstractFinderRepository implements UrlRewriteRepositoryInterface
38
{
39
40
    /**
41
     * The registry processor instance.
42
     *
43
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
44
     */
45
    protected $registryProcessor;
46
47
    /**
48
     * The prepared statement to load the existing URL rewrites.
49
     *
50
     * @var \PDOStatement
51
     */
52
    protected $urlRewritesStmt;
53
54
    /**
55
     * The prepared statement to load the existing URL rewrites by their entity type and ID.
56
     *
57
     * @var \PDOStatement
58
     */
59
    protected $urlRewritesByEntityTypeAndEntityIdStmt;
60
61
    /**
62
     * The prepared statement to load the existing URL rewrites by their entity type, entity and store ID.
63
     *
64
     * @var \PDOStatement
65
     */
66
    protected $urlRewritesByEntityTypeAndEntityIdAndStoreIdStmt;
67
68
    /**
69
     * The prefix to load the URL rewrites with the given request path and store ID from the registry.
70
     *
71
     * @var string
72
     */
73
    protected $urlRewritesByRequestPathAndStoreIdPrefix;
74
75
    /**
76
     * Initializes the repository's prepared statements.
77
     *
78
     * @return void
79
     */
80
    public function init()
81
    {
82
83
        // initialize the prepared statements
84
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::STORES));
85
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITES));
86
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITE_BY_REQUEST_PATH_AND_STORE_ID));
87
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID));
88
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID_AND_STORE_ID));
89
    }
90
91
    /**
92
     * Return's the finder's entity name.
93
     *
94
     * @return string The finder's entity name
95
     */
96
    public function getEntityName()
97
    {
98
        return CacheKeys::URL_REWRITE;
99
    }
100
101
    /**
102
     * Return's the primary key name of the entity.
103
     *
104
     * @return string The name of the entity's primary key
105
     */
106
    public function getPrimaryKeyName()
107
    {
108
        return MemberNames::URL_REWRITE_ID;
109
    }
110
111
    /**
112
     * Return's an array with the available URL rewrites.
113
     *
114
     * @return array The available URL rewrites
115
     */
116
    public function findAll()
117
    {
118
        foreach ($this->getFinder(SqlStatementKeys::URL_REWRITES)->find() as $result) {
119
            yield $result;
120
        }
121
    }
122
123
    /**
124
     * Return's an array with the available URL rewrites
125
     *
126
     * @return array The array with the rewrites, grouped by request path and store ID
127
     * @todo Refactor to a yielded version also
128
     */
129
    public function findAllGroupedByRequestPathAndStoreId()
130
    {
131
132
        // initialize the array with the available URL rewrites
133
        $urlRewrites = array();
134
135
        // iterate over all available URL rewrites
136
        foreach ($this->findAll() as $urlRewrite) {
137
            // append the URL rewrite for the given request path and store ID combination
138
            $urlRewrites[$urlRewrite[MemberNames::REQUEST_PATH]][$urlRewrite[MemberNames::STORE_ID]] = $urlRewrite;
139
        }
140
141
        // return the array with the URL rewrites
142
        return $urlRewrites;
143
    }
144
145
    /**
146
     * Return's an array with the URL rewrites for the passed entity type and ID.
147
     *
148
     * @param string  $entityType The entity type to load the URL rewrites for
149
     * @param integer $entityId   The entity ID to load the URL rewrites for
150
     *
151
     * @return array The URL rewrites
152
     */
153 View Code Duplication
    public function findAllByEntityTypeAndEntityId($entityType, $entityId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
156
        // initialize the params
157
        $params = array(
158
            MemberNames::ENTITY_TYPE => $entityType,
159
            MemberNames::ENTITY_ID   => $entityId
160
        );
161
162
        // load and return the URL rewrites
163
        foreach ($this->getFinder(SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID)->find($params) as $result) {
164
            yield $result;
165
        }
166
    }
167
168
    /**
169
     * Return's an array with the URL rewrites for the passed entity type, entity and store ID.
170
     *
171
     * @param string  $entityType The entity type to load the URL rewrites for
172
     * @param integer $entityId   The entity ID to load the URL rewrites for
173
     * @param integer $storeId    The store ID to load the URL rewrites for
174
     *
175
     * @return array The URL rewrites
176
     */
177 View Code Duplication
    public function findAllByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
180
        // initialize the params
181
        $params = array(
182
            MemberNames::ENTITY_TYPE => $entityType,
183
            MemberNames::ENTITY_ID   => $entityId,
184
            MemberNames::STORE_ID    => $storeId
185
        );
186
187
        // load and return the URL rewrites
188
        foreach ($this->getFinder(SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID_AND_STORE_ID)->find($params) as $result) {
189
            yield $result;
190
        }
191
    }
192
193
    /**
194
     * Load's and return's the URL rewrite for the given request path and store ID.
195
     *
196
     * @param string $requestPath The request path to load the URL rewrite for
197
     * @param int    $storeId     The store ID to load the URL rewrite for
198
     *
199
     * @return array|null The URL rewrite found for the given request path and store ID
200
     */
201
    public function findOneByRequestPathAndStoreId(string $requestPath, int $storeId)
202
    {
203
204
        // initialize the params
205
        $params = array(
206
            MemberNames::REQUEST_PATH => $requestPath,
207
            MemberNames::STORE_ID     => $storeId
208
        );
209
210
        // load and return the URL rewrite with the passed request path and store ID
211
        return $this->getFinder(SqlStatementKeys::URL_REWRITE_BY_REQUEST_PATH_AND_STORE_ID)->find($params);
212
    }
213
}
214