ClearCustomerObserver::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 22
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Observers\ClearCustomerObserver
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\Observers;
16
17
use TechDivision\Import\Customer\Utils\ColumnKeys;
18
use TechDivision\Import\Customer\Utils\MemberNames;
19
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
20
21
/**
22
 * Observer that removes the customer with the identifier found in the CSV file.
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 ClearCustomerObserver extends AbstractCustomerImportObserver
31
{
32
33
    /**
34
     * The customer bunch processor instance.
35
     *
36
     * @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface
37
     */
38
    protected $customerBunchProcessor;
39
40
    /**
41
     * Initialize the observer with the passed customer bunch processor instance.
42
     *
43
     * @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The customer bunch processor instance
44
     */
45
    public function __construct(CustomerBunchProcessorInterface $customerBunchProcessor)
46
    {
47
        $this->customerBunchProcessor = $customerBunchProcessor;
48
    }
49
50
    /**
51
     * Return's the customer bunch processor instance.
52
     *
53
     * @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The customer bunch processor instance
54
     */
55
    protected function getCustomerBunchProcessor()
56
    {
57
        return $this->customerBunchProcessor;
58
    }
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return array The processed row
64
     */
65
    protected function process()
66
    {
67
68
        // load email and website code
69
        $email = $this->getValue(ColumnKeys::EMAIL);
70
        $website = $this->getValue(ColumnKeys::WEBSITE);
71
72
        // query whether or not, we've found a customer identifier => means we've found a new customer
73
        if ($this->isLastIdentifier(array($email, $website))) {
74
            return;
75
        }
76
77
        // delete the customer with the passed identifier
78
        $this->deleteCustomer(
79
            array(
80
                MemberNames::EMAIL      => $email,
81
                MemberNames::WEBSITE_ID => $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE))
82
            )
83
        );
84
85
        // flush the cache to remove the deleted customer (which has previously been cached)
86
        $this->getCustomerBunchProcessor()->cleanUp();
87
    }
88
89
    /**
90
     * Delete's the entity with the passed attributes.
91
     *
92
     * @param array       $row  The attributes of the entity to delete
93
     * @param string|null $name The name of the prepared statement that has to be executed
94
     *
95
     * @return void
96
     */
97
    protected function deleteCustomer($row, $name = null)
98
    {
99
        $this->getCustomerBunchProcessor()->deleteCustomer($row, $name);
100
    }
101
102
    /**
103
     * Return's the store website for the passed code.
104
     *
105
     * @param string $code The code of the store website to return the ID for
106
     *
107
     * @return integer The store website ID
108
     * @throws \Exception Is thrown, if the store website with the requested code is not available
109
     */
110
    protected function getStoreWebsiteIdByCode($code)
111
    {
112
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
113
    }
114
}
115