BaseFixture::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Bex\Behat\Magento2InitExtension\Fixtures;
4
5
use Magento\Framework\App\ObjectManager;
6
use Magento\Framework\App\State;
7
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
8
use Magento\Framework\Registry;
9
10
abstract class BaseFixture
11
{
12
    /**
13
     * @var ObjectManager
14
     */
15
    private $objectManager;
16
17
    /**
18
     * init object manager and set app state
19
     */
20
    public function __construct()
21
    {
22
        $this->initObjectManager();
23
        $this->initAppState();
24
    }
25
26
    /**
27
     * @return ObjectManager
28
     */
29
    private function initObjectManager()
30
    {
31
        if (is_null($this->objectManager)) {
32
            $this->objectManager = ObjectManager::getInstance();
33
        }
34
        
35
        return $this->objectManager;
36
    }
37
38
    /**
39
     * init app state (add adminhtml configs as well)
40
     */
41
    private function initAppState()
42
    {
43
        $appState = $this->getMagentoObject(State::class);
44
        $configLoader = $this->getMagentoObject(ConfigLoaderInterface::class);
45
        $registry = $this->getMagentoObject(Registry::class);
46
        
47
        try {
48
            $appState->getAreaCode();
49
        } catch (\Exception $e) {
50
            $appState->setAreaCode('adminhtml');
51
            $this->objectManager->configure($configLoader->load('adminhtml'));
52
            $registry->register('isSecureArea', true);
53
        }
54
    }
55
56
    /**
57
     * @param  string $type
58
     *
59
     * @return mixed
60
     */
61
    protected function getMagentoObject($type)
62
    {
63
        return $this->objectManager->get($type);
64
    }
65
66
    /**
67
     * @param  string $type
68
     * @param  array  $arguments
69
     *
70
     * @return mixed
71
     */
72
    protected function createMagentoObject($type, array $arguments = [])
73
    {
74
        return $this->objectManager->create($type, $arguments);
75
    }
76
77
    /**
78
     * @return ObjectManager
79
     */
80
    protected function getObjectManager()
81
    {
82
        return $this->objectManager;
83
    }
84
}
85