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   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 82
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B sendEmail() 0 24 3
B sendToAllEmailsFromList() 0 27 4
B getActualNewsForSubsctibe() 0 26 4
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