Completed
Push — master ( 727596...fd8695 )
by Tim
16s queued 11s
created

CacheUrlRewriteListener::getEntityName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Listeners\CacheUrlRewriteListener
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 2021 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\Listeners;
22
23
use League\Event\EventInterface;
24
use League\Event\AbstractListener;
25
use TechDivision\Import\Utils\CacheKeys;
26
use TechDivision\Import\Utils\MemberNames;
27
use TechDivision\Import\Utils\EntityStatus;
28
use TechDivision\Import\Utils\SqlStatementKeys;
29
use TechDivision\Import\Cache\CacheAdapterInterface;
30
use TechDivision\Import\Dbal\Actions\CachedActionInterface;
31
32
/**
33
 * A listener implementation that updates the cache after a row has been updated.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2021 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import
39
 * @link      http://www.techdivision.com
40
 */
41
class CacheUrlRewriteListener extends AbstractListener
42
{
43
44
    /**
45
     * The cache adapter instance.
46
     *
47
     * @var \TechDivision\Import\Cache\CacheAdapterInterface
48
     */
49
    protected $cacheAdapter;
50
51
    /**
52
     * Initializes the listener with the cache adapter and the system loggers.
53
     *
54
     * @param \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter The cache adapter instance
55
     */
56
    public function __construct(CacheAdapterInterface $cacheAdapter)
57
    {
58
        $this->cacheAdapter = $cacheAdapter;
59
    }
60
61
    /**
62
     * Return's the finder's entity name.
63
     *
64
     * @return string The finder's entity name
65
     */
66
    public function getEntityName()
67
    {
68
        return CacheKeys::URL_REWRITE;
69
    }
70
71
    /**
72
     * Return's the primary key name of the entity.
73
     *
74
     * @return string The name of the entity's primary key
75
     */
76
    public function getPrimaryKeyName()
77
    {
78
        return MemberNames::URL_REWRITE_ID;
79
    }
80
81
    /**
82
     * Return's the finder's unique key.
83
     *
84
     * @return string The unique key
85
     */
86
    public function getKey()
87
    {
88
        return SqlStatementKeys::URL_REWRITE_BY_REQUEST_PATH_AND_STORE_ID;
89
    }
90
91
    /**
92
     * Handle the event.
93
     *
94
     * @param \League\Event\EventInterface                            $event  The event that triggered the listener
95
     * @param \TechDivision\Import\Dbal\Actions\CachedActionInterface $action The action instance that triggered the event
96
     * @param array                                                   $row    The row to be cached
97
     *
98
     * @return void
99
     */
100
    public function handle(EventInterface $event, CachedActionInterface $action = null, array $row = array())
101
    {
102
103
        // prepare the unique cache key for the EAV attribute option value
104
        $uniqueKey = array($this->getEntityName() => $row[$this->getPrimaryKeyName()]);
105
106
        // initialize the params
107
        $params = array(
108
            MemberNames::REQUEST_PATH => $row[MemberNames::REQUEST_PATH],
109
            MemberNames::STORE_ID     => $row[MemberNames::STORE_ID]
110
        );
111
112
        // prepare the cache key
113
        $cacheKey = $this->cacheAdapter->cacheKey(array($this->getKey() => $params), false);
114
115
        // query whether or not which status the passed entity has
116
        switch ($row[EntityStatus::MEMBER_NAME]) {
117
            case EntityStatus::STATUS_CREATE:
118
                // in case we've a new entity, add it to the cache adapter
119
                $this->cacheAdapter->toCache($uniqueKey, $row, array($cacheKey => $uniqueKey));
0 ignored issues
show
Documentation introduced by
$uniqueKey is of type array<string,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
                break;
121
            case EntityStatus::STATUS_UPDATE:
122
                // in case we've an existing one, update it
123
                $this->cacheAdapter->toCache($uniqueKey, $row, array($cacheKey => $uniqueKey), array(), true);
0 ignored issues
show
Documentation introduced by
$uniqueKey is of type array<string,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
                break;
125
            default:
126
                // in all other cases, remove the existing entity from the cache to allow reloading it
127
                if ($this->cacheAdapter->isCached($uniqueKey)) {
0 ignored issues
show
Documentation introduced by
$uniqueKey is of type array<string,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
                    $this->cacheAdapter->removeCache($uniqueKey);
0 ignored issues
show
Documentation introduced by
$uniqueKey is of type array<string,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
                }
130
        }
131
    }
132
}
133