Completed
Push — develop ( 9f7296...7c60a0 )
by Carlo
03:36 queued 12s
created

Scope::switchScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Magefix\Magento\Store;
4
5
use Mage;
6
use Mage_Core_Model_App;
7
8
/**
9
 * Class Scope
10
 *
11
 * Provides functionality to switch Magento store scope during fixtures building
12
 *
13
 * @package Magefix\Magento\Store
14
 * @author  Carlo Tasca <[email protected]>
15
 */
16
class Scope
17
{
18
    private static $_currentStore;
19
20
    /**
21
     * Register current store id and switch to admin store scope
22
     */
23
    public static function setAdminStoreScope()
24
    {
25
        self::$_currentStore = Mage::app()->getStore()->getId();
26
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
27
    }
28
29
    /**
30
     * Switch to current store scope
31
     */
32
    public static function setCurrentStoreScope()
33
    {
34
        Mage::app()->setCurrentStore(self::$_currentStore);
35
    }
36
37
    /**
38
     * @param $storeCode
39
     * @throws \Exception
40
     */
41
    public static function switchScope($storeCode)
42
    {
43
        $stores = Mage::getModel('core/store')->getCollection()
44
            ->addFieldToFilter('code', ['eq' => $storeCode]);
45
46
        $store = $stores->getSelect()->limit(1)->query()->fetch();
47
48
        if (empty($store)) {
49
            throw new \Exception("Unavailable data for store code: '{$storeCode}'");
50
        }
51
52
        Mage::app()->setCurrentStore($store['store_id']);
53
    }
54
}
55