Completed
Push — 1.9 ( 879786...89c35b )
by
unknown
15:34
created

updateReminderTemplates()   B

Complexity

Conditions 3
Paths 10

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 33
rs 8.8571
cc 3
eloc 26
nc 10
nop 6
1
<?php
2
3
namespace OroCRM\Bundle\TaskBundle\Migrations\Schema\v1_10;
4
5
use Doctrine\DBAL\ConnectionException;
6
7
use Psr\Log\LoggerInterface;
8
9
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
10
11
class UpdateReminderEmailTemplates extends ParametrizedMigrationQuery
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getDescription()
17
    {
18
        return 'Update date format in reminder email templates using recipient organization localization settings';
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function execute(LoggerInterface $logger)
25
    {
26
        $dateFilterPattern = "|date('F j, Y, g:i A')";
27
        $calendarRangePattern[] = 'calendar_date_range(entity.start, entity.end, entity.allDay, 1)';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$calendarRangePattern was never initialized. Although not strictly required by PHP, it is generally a good practice to add $calendarRangePattern = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
28
        $calendarRangePattern[] = "calendar_date_range(entity.start, entity.end, entity.allDay, 'F j, Y', 1)";
29
        $dateFilterReplacement = "|oro_format_datetime_organization({'organization': entity.organization})";
30
        $calendarRangeReplacement = 'calendar_date_range_organization(entity.start,' .
31
            ' entity.end, entity.allDay, 1, null, null, null, entity.organization)';
32
        $this->updateReminderTemplates(
33
            $logger,
34
            'task_reminder',
35
            $dateFilterPattern,
36
            $dateFilterReplacement,
37
            $calendarRangePattern,
38
            $calendarRangeReplacement
39
        );
40
    }
41
42
    /**
43
     * @param LoggerInterface $logger
44
     * @param string          $templateName
45
     * @param string|array    $dateFilterPattern
46
     * @param string          $dateFilterReplacement
47
     * @param string|array    $calendarRangePattern
48
     * @param string          $calendarRangeReplacement
49
     *
50
     * @throws ConnectionException
51
     * @throws \Exception
52
     */
53
    protected function updateReminderTemplates(
54
        LoggerInterface $logger,
55
        $templateName,
56
        $dateFilterPattern,
57
        $dateFilterReplacement,
58
        $calendarRangePattern,
59
        $calendarRangeReplacement
60
    ) {
61
        $sql = 'SELECT * FROM oro_email_template WHERE name = :name ORDER BY id';
62
        $parameters = ['name' => $templateName];
63
        $types = ['name' => 'string'];
64
65
        $this->logQuery($logger, $sql, $parameters, $types);
66
        $templates = $this->connection->fetchAll($sql, $parameters, $types);
67
68
        try {
69
            $this->connection->beginTransaction();
70
            foreach ($templates as $template) {
71
                $subject = str_replace($dateFilterPattern, $dateFilterReplacement, $template['subject']);
72
                $content = str_replace($dateFilterPattern, $dateFilterReplacement, $template['content']);
73
                $content = str_replace($calendarRangePattern, $calendarRangeReplacement, $content);
74
                $this->connection->update(
75
                    'oro_email_template',
76
                    ['subject' => $subject, 'content' => $content],
77
                    ['id' => $template['id']]
78
                );
79
            }
80
            $this->connection->commit();
81
        } catch (\Exception $e) {
82
            $this->connection->rollBack();
83
            throw $e;
84
        }
85
    }
86
}
87