Failed Conditions
Pull Request — develop (#4)
by Carlo
03:06
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
                $mageModel = $this->getMageModelForMatch($entryMatch[1]);
32
                if (is_a($mageModel, 'Mage_Core_Model_Abstract')) {
33
                    $this->_echoRegistryChangeMessage(
34
                        $mageModel, $entryMatch[1], $entry, $key
0 ignored issues
show
Bug introduced by
It seems like $mageModel defined by $this->getMageModelForMatch($entryMatch[1]) on line 31 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?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

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