Skip to content

Commit 2c63365

Browse files
committed
Трэйты для миграций.
1 parent 07e8b25 commit 2c63365

File tree

11 files changed

+1013
-0
lines changed

11 files changed

+1013
-0
lines changed

Services/Facades/Container.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Facades;
4+
5+
use Prokl\FacadeBundle\Services\AbstractFacade;
6+
7+
/**
8+
* Class Container
9+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Facades
10+
*
11+
* @method static void set(string $id, ?object $service)
12+
* @method static object|null get($id, int $invalidBehavior)
13+
* @method static bool has($id)
14+
* @method static array|bool|float|int|string|null getParameter(string $name)
15+
* @method static bool hasParameter(string $name)
16+
* @method static void setParameter(string $name, $value)
17+
*/
18+
class Container extends AbstractFacade
19+
{
20+
/**
21+
* @inheritDoc
22+
*/
23+
protected static function getFacadeAccessor() : string
24+
{
25+
return 'service_container';
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Migrations;
4+
5+
/**
6+
* Class ClearCacheTrait
7+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Migrations
8+
*
9+
* @since 11.04.2021
10+
*/
11+
class ClearCacheTrait
12+
{
13+
/**
14+
* Очищает все виды кэша.
15+
*
16+
* @return void
17+
*/
18+
protected function clearCache() : void
19+
{
20+
global $USER_FIELD_MANAGER;
21+
if ($USER_FIELD_MANAGER) {
22+
$USER_FIELD_MANAGER->CleanCache();
23+
}
24+
25+
BXClearCache(true, '/');
26+
}
27+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Migrations;
4+
5+
use CEventType;
6+
use Exception;
7+
8+
/**
9+
* Trait EmailEventTrait
10+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Migrations
11+
*
12+
* @since 11.04.2021
13+
*/
14+
trait EmailEventTrait
15+
{
16+
/**
17+
* Создает новый тип почтовых событий.
18+
*
19+
* @param array $eventData
20+
*
21+
* @return array
22+
*
23+
* @throws Exception
24+
*/
25+
public function createEmailEventType(array $eventData): array
26+
{
27+
if ($this->findEmailEventType($eventData)) {
28+
throw new Exception("Email event type {$eventData['EVENT_NAME']} already exists");
29+
}
30+
31+
$res = CEventType::add($eventData);
32+
if (!$res) {
33+
throw new Exception("Can't create email event type {$eventData['EVENT_NAME']}: {$et->LAST_ERROR}");
34+
}
35+
36+
return ["Email event type {$eventData['EVENT_NAME']}({$res}) created"];
37+
}
38+
39+
/**
40+
* Обновляет тип почтовых событий.
41+
*
42+
* @param array $eventData
43+
*
44+
* @return array
45+
*
46+
* @throws Exception
47+
*/
48+
public function updateEmailEventType(array $eventData): array
49+
{
50+
if (!($event = $this->findEmailEventType($eventData))) {
51+
throw new Exception("Can't find {$eventData['EVENT_NAME']} email event type");
52+
}
53+
$et = new CEventType;
54+
unset($eventData['EVENT_NAME'], $eventData['LID']);
55+
$result = CEventType::update(['ID' => $event['ID']], $eventData);
56+
if (!$result) {
57+
throw new Exception("Can't update email event type {$eventData['EVENT_NAME']}: {$et->LAST_ERROR}");
58+
}
59+
60+
return ["Email event type {$event['EVENT_NAME']}({$event['ID']}) updated"];
61+
}
62+
63+
/**
64+
* Удаляет тип почтового события по его идентификатору (EVENT_NAME).
65+
*
66+
* @param array $eventData
67+
*
68+
* @return array
69+
* @throws Exception
70+
*/
71+
public function deleteEmailEventType(array $eventData): array
72+
{
73+
if (!($event = $this->findEmailEventType($eventData))) {
74+
throw new Exception("Can't find {$eventData['EVENT_NAME']} email event type");
75+
}
76+
77+
CEventType::delete(['ID' => $event['ID']]);
78+
79+
return ["Email event type {$eventData['EVENT_NAME']}({$event['ID']}) deleted"];
80+
}
81+
82+
/**
83+
* Ищет тип почтового события по массиву параметров.
84+
*
85+
* @param array $eventData
86+
*
87+
* @return array|null
88+
*
89+
* @throws Exception
90+
*/
91+
public function findEmailEventType(array $eventData): ?array
92+
{
93+
if (empty($eventData['EVENT_NAME'])) {
94+
throw new Exception('Empty email event type name');
95+
}
96+
$filter = ['TYPE_ID' => $eventData['EVENT_NAME']];
97+
if (!empty($eventData['LID'])) {
98+
$filter['LID'] = $eventData['LID'];
99+
}
100+
101+
return CEventType::getList($filter)->fetch();
102+
}
103+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Migrations;
4+
5+
use CEventMessage;
6+
use Exception;
7+
8+
/**
9+
* Trait EmailTemplateTrait
10+
* @package Local\Services\Utils\Migrations
11+
*
12+
* @since 11.04.2021
13+
*/
14+
trait EmailTemplateTrait
15+
{
16+
/**
17+
* Создает новый шаблон для почтового сообщения.
18+
*
19+
* @param array $templateData
20+
*
21+
* @return array
22+
*
23+
* @throws Exception
24+
*/
25+
public function createEmailTemplate(array $templateData): array
26+
{
27+
if ($this->findEmailTemplate($templateData)) {
28+
throw new Exception("Email template for event {$templateData['EVENT_NAME']} already exists");
29+
}
30+
31+
$et = new CEventMessage;
32+
$res = $et->add($templateData);
33+
if (!$res) {
34+
throw new Exception("Can't create email event type {$templateData['EVENT_NAME']}: {$et->LAST_ERROR}");
35+
}
36+
37+
return ["Email template({$res}) for event {$templateData['EVENT_NAME']} created"];
38+
}
39+
40+
/**
41+
* Обновляет шаблон почтового сообщения.
42+
*
43+
* @param array $templateData
44+
* @param array $search
45+
*
46+
* @return array
47+
*
48+
* @throws Exception
49+
*/
50+
public function updateEmailTemplate(array $search, array $templateData): array
51+
{
52+
if (!($template = $this->findEmailTemplate($search))) {
53+
throw new Exception("Email template for event {$templateData['EVENT_NAME']} not found");
54+
}
55+
56+
$et = new CEventMessage;
57+
unset($templateData['EVENT_NAME'], $templateData['LID']);
58+
$res = $et->update($template['ID'], $templateData);
59+
if (!$res) {
60+
throw new Exception("Can't update template for event {$templateData['EVENT_NAME']}: {$et->LAST_ERROR}");
61+
}
62+
63+
return ["Email template({$template['ID']}) for event {$template['EVENT_NAME']} updated"];
64+
}
65+
66+
/**
67+
* Удаляет шаблон почтового сообщения.
68+
*
69+
* @param array $templateData
70+
*
71+
* @return array
72+
* @throws Exception
73+
*/
74+
public function deleteEmailTemplate(array $templateData): array
75+
{
76+
if (!($template = $this->findEmailTemplate($templateData))) {
77+
throw new Exception("Email template for event {$templateData['EVENT_NAME']} not found");
78+
}
79+
80+
$et = new CEventMessage;
81+
$res = $et->delete($template['ID']);
82+
83+
if (!$res) {
84+
throw new Exception("Can't delete template({$template['ID']}) for type {$templateData['EVENT_NAME']}");
85+
}
86+
87+
return ["Email template({$template['ID']}) for type {$templateData['EVENT_NAME']} deleted"];
88+
}
89+
90+
/**
91+
* Ищет шаблон почтового сообщения по массиву параметров.
92+
*
93+
* @param array $templateData
94+
*
95+
* @return array|null
96+
*
97+
* @throws Exception
98+
*/
99+
public function findEmailTemplate(array $templateData): ?array
100+
{
101+
if (empty($templateData['EVENT_NAME'])) {
102+
throw new Exception('Empty email event type name');
103+
}
104+
105+
$filter = ['TYPE_ID' => $templateData['EVENT_NAME']];
106+
if (!empty($templateData['LID'])) {
107+
$filter['SITE_ID'] = $templateData['LID'];
108+
}
109+
if (!empty($templateData['SUBJECT'])) {
110+
$filter['SUBJECT'] = $templateData['SUBJECT'];
111+
}
112+
113+
$return = null;
114+
$rsMess = CEventMessage::GetList(
115+
($by = 'site_id'),
116+
($order = 'desc'),
117+
$filter
118+
);
119+
while ($template = $rsMess->fetch()) {
120+
if ($return) {
121+
throw new Exception("More than one template are found: {$templateData['EVENT_NAME']}");
122+
}
123+
$return = $template;
124+
}
125+
126+
return $return;
127+
}
128+
}

0 commit comments

Comments
 (0)