Completed
Push — pac-264--pdo-exception ( 69b20a )
by Tim
05:41
created

findOneByUrlRewriteByRequestPathAndStoreId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 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\ScopeKeys;
24
use TechDivision\Import\Utils\CacheKeys;
25
use TechDivision\Import\Utils\MemberNames;
26
use TechDivision\Import\Utils\RegistryKeys;
27
use TechDivision\Import\Utils\SqlStatementKeys;
28
use TechDivision\Import\Utils\CoreConfigDataKeys;
29
use TechDivision\Import\Loaders\LoaderInterface;
30
use TechDivision\Import\Connection\ConnectionInterface;
31
use TechDivision\Import\Services\RegistryProcessorInterface;
32
use TechDivision\Import\Repositories\Finders\FinderFactoryInterface;
33
34
/**
35
 * Repository implementation to load URL rewrite data.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2016 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import
41
 * @link      http://www.techdivision.com
42
 */
43
class UrlRewriteRepository extends AbstractFinderRepository implements UrlRewriteRepositoryInterface
44
{
45
46
    /**
47
     * The registry processor instance.
48
     *
49
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
50
     */
51
    protected $registryProcessor;
52
53
    /**
54
     * The prepared statement to load the existing URL rewrites.
55
     *
56
     * @var \PDOStatement
57
     */
58
    protected $urlRewritesStmt;
59
60
    /**
61
     * The prepared statement to load the existing URL rewrites by their entity type and ID.
62
     *
63
     * @var \PDOStatement
64
     */
65
    protected $urlRewritesByEntityTypeAndEntityIdStmt;
66
67
    /**
68
     * The prepared statement to load the existing URL rewrites by their entity type, entity and store ID.
69
     *
70
     * @var \PDOStatement
71
     */
72
    protected $urlRewritesByEntityTypeAndEntityIdAndStoreIdStmt;
73
74
    /**
75
     * The prefix to load the URL rewrites with the given request path and store ID from the registry.
76
     *
77
     * @var string
78
     */
79
    protected $urlRewritesByRequestPathAndStoreIdPrefix;
80
81
    /**
82
     * The core config data loader instance.
83
     *
84
     * @var \TechDivision\Import\Loaders\LoaderInterface
85
     */
86
    protected $coreConfigDataLoader;
87
88
89
    /**
90
     * The array with the entity type > configuration key mapping.
91
     *
92
     * @var array
93
     */
94
    protected $entityTypeConfigKeyMapping = array(
95
        'product'  => CoreConfigDataKeys::CATALOG_SEO_PRODUCT_URL_SUFFIX,
96
        'category' => CoreConfigDataKeys::CATALOG_SEO_CATEGORY_URL_SUFFIX
97
    );
98
99
    /**
100
     * The array with the entity type and store view specific suffixes.
101
     *
102
     * @var array
103
     */
104
    protected $suffixes = array();
105
106
    /**
107
     * Initialize the repository with the passed connection and utility class name.
108
     * .
109
     * @param \TechDivision\Import\Connection\ConnectionInterface               $connection             The connection instance
110
     * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance
111
     * @param \TechDivision\Import\Repositories\Finders\FinderFactoryInterface  $finderFactory          The finder factory instance
112
     * @param \TechDivision\Import\Services\RegistryProcessorInterface          $registryProcessor      The registry processor instance
113
     * @param \TechDivision\Import\Loaders\LoaderInterface                      $coreConfigDataLoader   The core config data loader instance
114
     */
115
    public function __construct(
116
        ConnectionInterface $connection,
117
        SqlStatementRepositoryInterface $sqlStatementRepository,
118
        FinderFactoryInterface $finderFactory,
119
        RegistryProcessorInterface $registryProcessor,
120
        LoaderInterface $coreConfigDataLoader
121
    ) {
122
123
        // set the registry processor and the core config data loader instances
124
        $this->registryProcessor = $registryProcessor;
125
        $this->coreConfigDataLoader = $coreConfigDataLoader;
126
127
        // pass the connection, SQL statement repository and the finder factory to the parent class
128
        parent::__construct($connection, $sqlStatementRepository, $finderFactory);
129
    }
130
131
    /**
132
     * Initializes the repository's prepared statements.
133
     *
134
     * @return void
135
     */
136
    public function init()
137
    {
138
139
        // initialize the prepared statements
140
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::STORES));
141
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITES));
142
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID));
143
        $this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID_AND_STORE_ID));
144
145
        // initialize the prefix to load the URL rewrites with the
146
        // given request path and store ID from the registry
147
        $this->urlRewritesByRequestPathAndStoreIdPrefix = sprintf(
148
            '%s.%s.%s',
149
            RegistryKeys::STATUS,
150
            RegistryKeys::GLOBAL_DATA,
151
            RegistryKeys::URL_REWRITES
152
        );
153
154
        // initialize the URL suffixs from the Magento core configuration
155
        foreach ($this->getFinder(SqlStatementKeys::STORES)->find() as $store) {
156
            // prepare the array with the entity type and store ID specific suffixes
157
            foreach ($this->entityTypeConfigKeyMapping as $entityType => $configKey) {
158
                // load the suffix for the given entity type => configuration key and store ID
159
                $suffix = $this->coreConfigDataLoader->load($configKey, '.html', ScopeKeys::SCOPE_DEFAULT, $storeId = $store[MemberNames::STORE_ID]);
0 ignored issues
show
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $configKey.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
160
                // register the suffux in the array
161
                $this->suffixes[$entityType][$storeId] = $suffix;
162
            }
163
        }
164
    }
165
166
    /**
167
     * Return's the finder's entity name.
168
     *
169
     * @return string The finder's entity name
170
     */
171
    public function getEntityName()
172
    {
173
        return CacheKeys::URL_REWRITE;
174
    }
175
176
    /**
177
     * Return's the primary key name of the entity.
178
     *
179
     * @return string The name of the entity's primary key
180
     */
181
    public function getPrimaryKeyName()
182
    {
183
        return MemberNames::URL_REWRITE_ID;
184
    }
185
186
    /**
187
     * Return's an array with the available URL rewrites.
188
     *
189
     * @return array The available URL rewrites
190
     */
191
    public function findAll()
192
    {
193
        foreach ($this->getFinder(SqlStatementKeys::URL_REWRITES)->find() as $result) {
194
            yield $result;
195
        }
196
    }
197
198
    /**
199
     * Return's an array with the available URL rewrites
200
     *
201
     * @return array The array with the rewrites, grouped by request path and store ID
202
     */
203
    public function findAllGroupedByRequestPathAndStoreId()
204
    {
205
206
        // initialize the array with the available URL rewrites
207
        $urlRewrites = array();
208
209
        // iterate over all available URL rewrites
210
        foreach ($this->findAll() as $urlRewrite) {
211
            // load the request path as we need it as key for the array
212
            $requestPath = $urlRewrite[MemberNames::REQUEST_PATH];
213
214
            // query whether or not a suffix has been registered for the given entity type
215
            // and store ID. If yes, we strip the suffix to have a unified access via the
216
            // stripped reqeust path  and the store ID
217
            if (isset($this->suffixes[$urlRewrite[MemberNames::ENTITY_TYPE]][$urlRewrite[MemberNames::STORE_ID]])) {
218
                // load the suffix from the available ones
219
                $suffix = $this->suffixes[$urlRewrite[MemberNames::ENTITY_TYPE]][$urlRewrite[MemberNames::STORE_ID]];
220
                // remove the suffix from the request path to unify access
221
                $requestPath = str_replace($suffix, null, $requestPath);
222
            }
223
224
            // initialize the array with the URL
225
            // rewrites for each request path
226
            if (isset($urlRewrites[$requestPath]) === false) {
227
                $urlRewrites[$requestPath] = array();
228
            }
229
230
            // append the URL rewrite for the given request path and store ID combination
231
            $urlRewrites[$requestPath][$urlRewrite[MemberNames::STORE_ID]] = $urlRewrite;
232
        }
233
234
        // return the array with the URL rewrites
235
        return $urlRewrites;
236
    }
237
238
    /**
239
     * Return's an array with the URL rewrites for the passed entity type and ID.
240
     *
241
     * @param string  $entityType The entity type to load the URL rewrites for
242
     * @param integer $entityId   The entity ID to load the URL rewrites for
243
     *
244
     * @return array The URL rewrites
245
     */
246 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...
247
    {
248
249
        // initialize the params
250
        $params = array(
251
            MemberNames::ENTITY_TYPE => $entityType,
252
            MemberNames::ENTITY_ID   => $entityId
253
        );
254
255
        // load and return the URL rewrites
256
        foreach ($this->getFinder(SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID)->find($params) as $result) {
257
            yield $result;
258
        }
259
    }
260
261
    /**
262
     * Return's an array with the URL rewrites for the passed entity type, entity and store ID.
263
     *
264
     * @param string  $entityType The entity type to load the URL rewrites for
265
     * @param integer $entityId   The entity ID to load the URL rewrites for
266
     * @param integer $storeId    The store ID to load the URL rewrites for
267
     *
268
     * @return array The URL rewrites
269
     */
270 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...
271
    {
272
273
        // initialize the params
274
        $params = array(
275
            MemberNames::ENTITY_TYPE => $entityType,
276
            MemberNames::ENTITY_ID   => $entityId,
277
            MemberNames::STORE_ID    => $storeId
278
        );
279
280
        // load and return the URL rewrites
281
        foreach ($this->getFinder(SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID_AND_STORE_ID)->find($params) as $result) {
282
            yield $result;
283
        }
284
    }
285
286
    /**
287
     * Load's and return's the URL rewrite for the given request path and store ID.
288
     *
289
     * ATTENTION: This method access the registry to make sure the parallel processes will access
290
     * the same URL rewrites. The initial data the will be added the registry will be loaded with
291
     * the method `UrlRewriteRepository::findAllGroupedByRequestPathAndStoreId()`
292
     *
293
     * @param string $requestPath The request path to load the URL rewrite for
294
     * @param int    $storeId     The store ID to load the URL rewrite for
295
     *
296
     * @return array|null The URL rewrite found for the given request path and store ID
297
     * @see \TechDivision\Import\Repositories\UrlRewriteRepository::findAllGroupedByRequestPathAndStoreId()
298
     */
299
    public function findOneByUrlRewriteByRequestPathAndStoreId(string $requestPath, int $storeId)
300
    {
301
        try {
302
            return $this->registryProcessor->load(sprintf('%s.%s.%d', $this->urlRewritesByRequestPathAndStoreIdPrefix, $requestPath, $storeId));
303
        } catch (\InvalidArgumentException $iae) {
304
            // do nothing here, because we simply
305
            // can't find the appropriate rewrite
306
        }
307
    }
308
}
309