GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — filters ( 0da638...f31b5a )
by
unknown
16:04
created

NewsletterSenderTrait::sendToAllEmailsFromList()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 8.5806
cc 4
eloc 18
nc 5
nop 0
1
<?php
2
3
namespace app\backgroundtasks\traits;
4
5
use app\modules\page\models\Page;
6
use app\models\SubscribeEmail;
7
use app\properties\HasProperties;
8
use Yii;
9
10
trait NewsletterSenderTrait
11
{
12
    public function sendEmail($id)
13
    {
14
        if (null === $id) {
15
            return;
16
        }
17
18
        $subscribeEmail = new SubscribeEmail();
19
        $pk = $subscribeEmail->primaryKey();
20
        $pk = $pk[0];
21
        if (null === $pk) {
22
            return;
23
        }
24
25
        $sEmail = $subscribeEmail->findOne([$pk => $id]);
26
        Yii::$app->mail->compose(
27
            '@app/views/notifications/newsletter/notify.php',
28
            [
29
                'name' => $sEmail->name
30
            ]
31
        )->setTo($sEmail->email)
32
            ->setFrom(Yii::$app->mail->transport->getUsername())
33
            ->setSubject(Yii::t('app', 'New info'))
34
            ->send();
35
    }
36
37
    public function sendToAllEmailsFromList()
38
    {
39
        $subscribeEmails = (new SubscribeEmail())->getActiveSubscribes();
40
41
        foreach ($subscribeEmails as $subscribeEmail) {
42
            $actualNews = $this->getActualNewsForSubsctibe($subscribeEmail);
43
            $sendStatus = false;
44
            if (count($actualNews) > 0) {
45
                $sendStatus = Yii::$app->mail->compose(
46
                    '@app/views/notifications/newsletter/notify.php',
47
                    [
48
                        'user' => $subscribeEmail->name,
49
                        'news' => $actualNews
50
                    ]
51
                )->setTo($subscribeEmail->email)
52
                    ->setFrom(Yii::$app->mail->transport->getUserName())
53
                    ->setSubject(Yii::t('app', 'Last news'))
54
                    ->send();
55
            }
56
57
            if ($sendStatus) {
58
                $nowFormat = date('Y-m-d H:i:s');
59
                $subscribeEmail->last_notify = $nowFormat;
60
                $subscribeEmail->save();
61
            }
62
        }
63
    }
64
65
    public function getActualNewsForSubsctibe($subscribe)
66
    {
67
        $actualNews = [];
68
        /** @var Page[]|HasProperties[] $pages */
69
        $pages = Page::find(
70
            [
71
                'is_deleted' => '0',
72
                'published' => '1'
73
            ]
74
        )->andWhere("date_added > '{$subscribe->last_notify}'")->all();
75
        if (count($pages) > 0) {
76
            foreach ($pages as $page) {
77
                $prop = $page->getPropertyValuesByKey("mailingType");
78
                if ($prop == 'news') {
79
                    $n = [
80
                        'name' => $page->h1,
81
                        'announce' => $page->announce,
82
                        'date_added' => $page->date_added
83
                    ];
84
                    array_push($actualNews, $n);
85
                }
86
            }
87
        }
88
89
        return $actualNews;
90
    }
91
}
92