Failed Conditions
Pull Request — develop (#4)
by Carlo
03:27
created

RegistryIterator::_echoRegistryChangeMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 4
1
<?php
2
3
namespace Magefix\Fixtures;
4
5
use Mage;
6
use Mage_Core_Model_Abstract;
7
use Magefix\Fixture\Factory\Builder;
8
9
/**
10
 * Class RegistryIterator
11
 *
12
 * @package Magefix\Fixtures
13
 * @author  Carlo Tasca <[email protected]>
14
 */
15
class RegistryIterator extends \ArrayObject
16
{
17
    /**
18
     * @param $hook
19
     *
20
     */
21
    public function iterateByHook($hook)
22
    {
23
        $registryIteratorIterator = $this->getIterator();
24
25
        while ($registryIteratorIterator->valid()) {
26
            $key        = $registryIteratorIterator->key();
27
            $entry      = $registryIteratorIterator->current();
28
            $entryMatch = $this->isEntryMatch($hook, $key);
29
30
            if (!empty($entryMatch) && isset($entryMatch[1])) {
31
                $this->_echoRegistryChangeMessage(
32
                    $this->getMageModelForMatch($entryMatch[1]), $entryMatch[1], $entry, $key
0 ignored issues
show
Bug introduced by
It seems like $this->getMageModelForMatch($entryMatch[1]) targeting Magefix\Fixtures\Registr...:getMageModelForMatch() can also be of type null; however, Magefix\Fixtures\Registr...RegistryChangeMessage() does only seem to accept object<Mage_Core_Model_Abstract>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
33
                );
34
            }
35
36
            $registryIteratorIterator->next();
37
        }
38
    }
39
40
    /**
41
     * @param $match
42
     *
43
     * @return \Mage_Catalog_Model_Category|\Mage_Catalog_Model_Product|\Mage_Customer_Model_Customer|\Mage_Sales_Model_Order|null
44
     *
45
     */
46
    public function getMageModelForMatch($match)
47
    {
48
        $model = null;
49
50
        if ($match == Builder::SIMPLE_PRODUCT_FIXTURE_TYPE || $match == Builder::CONFIGURABLE_PRODUCT_FIXTURE_TYPE
51
            || $match == Builder::BUNDLE_PRODUCT_FIXTURE_TYPE
52
        ) {
53
            $model = Mage::getModel('catalog/product');
54
        } elseif ($match == Builder::CATEGORY_FIXTURE_TYPE) {
55
            $model = Mage::getModel('catalog/category');
56
        } elseif ($match == Builder::CUSTOMER_FIXTURE_TYPE) {
57
            $model = Mage::getModel('customer/customer');
58
        } elseif ($match == Builder::SALES_ORDER_FIXTURE_TYPE) {
59
            $model = Mage::getModel('sales/order');
60
        }
61
62
        return $model;
63
    }
64
65
    /**
66
     * @param $hook
67
     * @param $key
68
     *
69
     * @return array
70
     *
71
     */
72
    public function isEntryMatch($hook, $key)
73
    {
74
        $matches = [];
75
        $matchOr = Builder::SIMPLE_PRODUCT_FIXTURE_TYPE . '|' . Builder::BUNDLE_PRODUCT_FIXTURE_TYPE
76
            . '|' . Builder::CONFIGURABLE_PRODUCT_FIXTURE_TYPE . '|' . Builder::CUSTOMER_FIXTURE_TYPE
77
            . '|' . Builder::CATEGORY_FIXTURE_TYPE . '|' . Builder::SALES_ORDER_FIXTURE_TYPE;
78
79
        preg_match("/^({$matchOr})_{$hook}/", $key, $matches);
80
81
        return $matches;
82
    }
83
84
    /**
85
     * @param Mage_Core_Model_Abstract $model
86
     * @param                          $fixtureType
87
     * @param                          $entry
88
     * @param                          $key
89
     */
90
    protected function _echoRegistryChangeMessage(Mage_Core_Model_Abstract $model, $fixtureType, $entry, $key)
91
    {
92
        echo $this->_deleteAndUnregisterFixture(
93
            $model, $fixtureType, $entry, $key
94
        );
95
    }
96
97
    /**
98
     * @param Mage_Core_Model_Abstract $model
99
     * @param                          $fixtureType
100
     * @param                          $entry
101
     * @param                          $key
102
     *
103
     * @return string
104
     * @throws \Exception
105
     */
106
    protected function _deleteAndUnregisterFixture(Mage_Core_Model_Abstract $model, $fixtureType, $entry, $key)
107
    {
108
        $fixture = $model->load((int)$entry);
109
        $fixture->delete();
110
        Builder::unregister($key);
111
112
        return $this->_deleteFixtureMessage($fixtureType, $entry);
113
    }
114
115
    /**
116
     * @param string $fixtureType
117
     * @param string $entry
118
     *
119
     * @return string
120
     */
121
    protected function _deleteFixtureMessage($fixtureType, $entry)
122
    {
123
        return "-- DELETED {$fixtureType} fixture with ID {$entry}\n";
124
    }
125
}
126