OnBeforeHLBlockUpdate::getReplace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixMigrations\Autocreate\Handlers;
4
5
use Arrilot\BitrixMigrations\Exceptions\SkipHandlerException;
6
use Bitrix\Highloadblock\HighloadBlockTable;
7
use Bitrix\Main\Entity\Event;
8
9
class OnBeforeHLBlockUpdate extends BaseHandler implements HandlerInterface
10
{
11
    /**
12
     * @var Event
13
     */
14
    protected $event;
15
16
    /**
17
     * HLBlock id.
18
     *
19
     * @var int
20
     */
21
    protected $id;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param array $params
27
     *
28
     * @throws SkipHandlerException
29
     */
30
    public function __construct($params)
31
    {
32
        $this->event = $params[0];
33
34
        $eventParams = $this->event->getParameters();
35
36
        $this->fields = $eventParams['fields'];
37
        $this->id = $eventParams['id']['ID'];
38
39
        if (!$this->fieldsHaveBeenChanged()) {
40
            throw new SkipHandlerException();
41
        }
42
    }
43
44
    /**
45
     * Get migration name.
46
     *
47
     * @return string
48
     */
49
    public function getName()
50
    {
51
        return 'auto_update_hlblock_'.$this->fields['TABLE_NAME'];
52
    }
53
54
    /**
55
     * Get template name.
56
     *
57
     * @return string
58
     */
59
    public function getTemplate()
60
    {
61
        return 'auto_update_hlblock';
62
    }
63
64
    /**
65
     * Get array of placeholders to replace.
66
     *
67
     * @return array
68
     */
69
    public function getReplace()
70
    {
71
        return [
72
            'id'     => $this->id,
73
            'fields' => var_export($this->fields, true),
74
        ];
75
    }
76
77
    /**
78
     * Determine if fields have been changed.
79
     *
80
     * @return bool
81
     */
82
    protected function fieldsHaveBeenChanged()
83
    {
84
        $old = HighloadBlockTable::getById($this->id)->fetch();
85
        $new = $this->fields + ['ID' => (string) $this->id];
86
87
        return $new != $old;
88
    }
89
}
90