Completed
Pull Request — master (#109)
by Tim
16:34
created

ProductCacheWarmer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Repositories\CacheWarmer\ProductCacheWarmer
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\Product\Repositories\CacheWarmer;
22
23
use TechDivision\Import\Product\Utils\MemberNames;
24
use TechDivision\Import\Product\Repositories\ProductRepositoryInterface;
25
use TechDivision\Import\Repositories\CacheWarmer\CacheWarmerInterface;
26
27
/**
28
 * Cache warmer implementation that pre-load the available products.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductCacheWarmer implements CacheWarmerInterface
37
{
38
39
    /**
40
     * The repository with the cache that has to be warmed.
41
     *
42
     * @var \TechDivision\Import\Product\Repositories\ProductRepositoryInterface
43
     */
44
    protected $repository;
45
46
    /**
47
     * Initialize the cache warmer with the repository that has to be warmed.
48
     *
49
     * @param \TechDivision\Import\Product\Repositories\ProductRepositoryInterface $repository The repository to warm
50
     */
51
    public function __construct(ProductRepositoryInterface $repository)
52
    {
53
        $this->repository = $repository;
54
    }
55
56
    /**
57
     * Warms the cache for the passed repository.
58
     *
59
     * @return void
60
     */
61
    public function warm()
62
    {
63
64
        // prepare the caches for the statements
65
        foreach ($this->repository->findAll() as $product) {
66
            // add the product to the cache, register the SKU reference as well
67
            $this->repository->toCache(
68
                $product[$this->repository->getPrimaryKeyName()],
69
                $product,
70
                array($product[MemberNames::SKU] => $product[$this->repository->getPrimaryKeyName()])
71
            );
72
        }
73
    }
74
}
75