RegistryIterator::_changeRegistryEntry()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Magefix\Fixtures;
4
5
use Mage;
6
use Mage_Core_Model_Abstract;
7
use Magefix\Fixture\Factory\Builder;
8
use Magefix\Magento\Model\Mapper;
9
10
/**
11
 * Class RegistryIterator
12
 *
13
 * @package Magefix\Fixtures
14
 * @author  Carlo Tasca <[email protected]>
15
 */
16
class RegistryIterator extends \ArrayObject
17
{
18
    /**
19
     * @param $hook
20
     *
21
     */
22
    public function iterateByHook($hook)
23
    {
24
        $registryIteratorIterator = $this->getIterator();
25
26
        while ($registryIteratorIterator->valid()) {
27
            $key = $registryIteratorIterator->key();
28
            $entry = $registryIteratorIterator->current();
29
            $entryMatch = $this->isEntryMatch($hook, $key);
30
31
            $this->_changeRegistryEntry($entryMatch, $entry, $key);
32
33
            $registryIteratorIterator->next();
34
        }
35
    }
36
37
    /**
38
     * @param string $match
39
     * @return Mage_Core_Model_Abstract
40
     */
41
    public function getMageModelForMatch($match)
42
    {
43
        $model = Mapper::map($match);
44
        return Mage::getModel($model);
45
    }
46
47
    /**
48
     * @param $hook
49
     * @param $key
50
     *
51
     * @return string[]
52
     *
53
     */
54
    public function isEntryMatch($hook, $key)
55
    {
56
        return RegistryEntryMatcher::match($hook, $key);
57
    }
58
59
    /**
60
     * @param                          $model
61
     * @param string $fixtureType
62
     * @param                          $entry
63
     * @param                          $key
64
     */
65
    protected function _echoRegistryChangeMessage($model, $fixtureType, $entry, $key)
66
    {
67
        echo $this->_deleteAndUnregisterFixture(
68
            $model, $fixtureType, $entry, $key
69
        );
70
    }
71
72
    /**
73
     * @param Mage_Core_Model_Abstract $model
74
     * @param string $fixtureType
75
     * @param                          $entry
76
     * @param                          $key
77
     *
78
     * @return string
79
     * @throws \Exception
80
     */
81
    protected function _deleteAndUnregisterFixture(Mage_Core_Model_Abstract $model, $fixtureType, $entry, $key)
82
    {
83
        $fixture = $model->load((int) $entry);
84
        $fixture->delete();
85
        Builder::unregister($key);
86
87
        return $this->_deleteFixtureMessage($fixtureType, $entry);
88
    }
89
90
    /**
91
     * @param string $fixtureType
92
     * @param string $entry
93
     *
94
     * @return string
95
     */
96
    protected function _deleteFixtureMessage($fixtureType, $entry)
97
    {
98
        return "-- DELETED {$fixtureType} fixture with ID {$entry}\n";
99
    }
100
101
    /**
102
     * @param array $entryMatch
103
     * @param       $entry
104
     * @param       $key
105
     *
106
     * @throws \Exception
107
     */
108
    protected function _changeRegistryEntry(array $entryMatch, $entry, $key)
109
    {
110
        $mageModel = $this->_instantiateMageModel($entryMatch);
111
112
        if ($this->_isExpectedType($mageModel)) {
113
            $this->_echoRegistryChangeMessage(
114
                $mageModel, $entryMatch[1], $entry, $key
115
            );
116
        }
117
    }
118
119
    /**
120
     * @param $mageModel
121
     *
122
     * @return bool
123
     *
124
     */
125
    protected function _isExpectedType($mageModel)
126
    {
127
        return !is_null($mageModel) && is_a($mageModel, 'Mage_Core_Model_Abstract');
128
    }
129
130
    /**
131
     * @param array $entryMatch
132
     *
133
     * @return array
134
     * @throws \Exception
135
     *
136
     */
137
    protected function _instantiateMageModel(array $entryMatch)
138
    {
139
        if (!isset($entryMatch[1])) {
140
            return;
141
        }
142
143
        $mageModel = $this->getMageModelForMatch($entryMatch[1]);
144
145
        if (is_null($mageModel)) {
146
            throw new \Exception('Cannot initialise Magento model from registry entry match.');
147
        }
148
149
        return $mageModel;
150
    }
151
}
152