AbstractMigration   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 140
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateTimeFormat() 0 4 1
A getDocumentRoot() 0 4 1
A getPersonalRoot() 0 4 1
A setSiteId() 0 8 2
A getSiteId() 0 4 1
A setSiteLanguageId() 0 8 2
A getSiteLanguageId() 0 4 1
B enableBitrixAPI() 0 27 2
A disableCacheIBlock() 0 6 1
1
<?php
2
3
namespace Rey\BitrixMigrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration as DoctrineAbstractMigration;
6
use Rey\BitrixMigrations\Exception\UnexpectedTypeException;
7
8
/**
9
 * Абстрактный класс для миграции
10
 * Содержит вспомогательные функции для работы с api Битрикса
11
 */
12
abstract class AbstractMigration extends DoctrineAbstractMigration
13
{
14
    /**
15
     * @var string
16
     */
17
    private $siteId = 's1';
18
19
    /**
20
     * @var string
21
     */
22
    private $siteLanguageId = 'ru';
23
24
    /**
25
     * Получить формат даты и времени
26
     *
27
     * @return string
28
     */
29
    protected function getDateTimeFormat()
30
    {
31
        return 'DD.MM.YYYY HH:MI:SS';
32
    }
33
34
    /**
35
     * Получить путь до корня проекта
36
     *
37
     * @return string
38
     */
39
    protected function getDocumentRoot()
0 ignored issues
show
Coding Style introduced by
getDocumentRoot uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        return $_SERVER['DOCUMENT_ROOT'];
42
    }
43
44
    /**
45
     * Получить путь к personal root сайта
46
     *
47
     * Переопределить метод получения путей до дириктории PersonalRoot
48
     * в зависимости от Id сайта ($this->getSiteId()) при многосайтовости.
49
     *
50
     * @return null|string
51
     */
52
    protected function getPersonalRoot()
0 ignored issues
show
Coding Style introduced by
getPersonalRoot uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
    {
54
        return $_SERVER['BX_PERSONAL_ROOT'];
55
    }
56
57
    /**
58
     * Установить Id сайта
59
     *
60
     * @param string $siteId
61
     *
62
     * @throws Rey\BitrixMigrations\Exception\UnexpectedTypeException Если арумент $siteId не строка
63
     */
64
    protected function setSiteId($siteId)
65
    {
66
        if (!is_string($siteId)) {
67
            throw new UnexpectedTypeException($siteId, 'string');
68
        }
69
70
        $this->siteId = $siteId;
71
    }
72
73
    /**
74
     * Получить Id сайта
75
     *
76
     * @return string
77
     */
78
    protected function getSiteId()
79
    {
80
        return $this->siteId;
81
    }
82
83
    /**
84
     * Установить идентификатор языковой версии сайта
85
     *
86
     * @param string $siteLanguageId
87
     *
88
     * @throws Rey\BitrixMigrations\Exception\UnexpectedTypeException Если арумент $siteLanguageId не строка
89
     */
90
    protected function setSiteLanguageId($siteLanguageId)
91
    {
92
        if (!is_string($siteLanguageId)) {
93
            throw new UnexpectedTypeException($siteLanguageId, 'string');
94
        }
95
96
        $this->siteLanguageId = $siteLanguageId;
97
    }
98
99
    /**
100
     * Получить идентификатор языковой версии сайта
101
     *
102
     * @return string
103
     */
104
    protected function getSiteLanguageId()
105
    {
106
        return $this->siteLanguageId;
107
    }
108
109
    /**
110
     * Подключить api Битрикса
111
     */
112
    protected function enableBitrixAPI()
0 ignored issues
show
Coding Style introduced by
enableBitrixAPI uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
113
    {
114
        global $DBType, $DBHost, $DBLogin, $DBPassword, $DBName, $DBDebug;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
115
116
        $_SERVER['DOCUMENT_ROOT'] = $this->getDocumentRoot();
117
        $_SERVER['BX_PERSONAL_ROOT'] = $this->getPersonalRoot();
118
        $_SERVER['HTTP_X_REAL_IP'] = '127.0.0.1';
119
120
        $siteId = $this->getSiteId();
121
        $siteLanguageId = $this->getSiteLanguageId();
122
123
        define('FORMAT_DATETIME', $this->getDateTimeFormat());
124
        define('SITE_ID', $siteId);
125
        define('LANG', $siteLanguageId);
126
        define('NO_KEEP_STATISTIC', true);
127
        define('NOT_CHECK_PERMISSIONS', true);
128
        define('BX_CLUSTER_GROUP', -1);
129
130
        $this->disableCacheIBlock();
131
132
        require $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php';
133
134
        //Подключение автозагрузчика Bitrix
135
        if (function_exists('\__autoload')) {
136
            spl_autoload_register('\__autoload');
137
        }
138
    }
139
140
    /**
141
     * Выключает кеширование инфоблоков, типов инфоблоков и свойств
142
     *
143
     * Решает проблему при создание типа инфоблока и добавление новых инфоблоков в одной миграции
144
     */
145
    private function disableCacheIBlock()
146
    {
147
        define('CACHED_b_iblock_type', false);
148
        define('CACHED_b_iblock', false);
149
        define('CACHED_b_iblock_property_enum', false);
150
    }
151
}
152