NewsitemListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace KI\PublicationBundle\Listener;
4
5
use Doctrine\ORM\EntityRepository;
6
use KI\PublicationBundle\Entity\Newsitem;
7
use KI\UserBundle\Entity\Achievement;
8
use KI\UserBundle\Entity\Club;
9
use KI\UserBundle\Event\AchievementCheckEvent;
10
use KI\UserBundle\Service\MailerService;
11
use KI\UserBundle\Service\NotifyService;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
class NewsitemListener
15
{
16
    protected $dispatcher;
17
    protected $mailerService;
18
    protected $notifyService;
19
    protected $userRepository;
20
21 View Code Duplication
    public function __construct(EventDispatcherInterface $dispatcher,
22
                                MailerService $mailerService,
23
                                NotifyService $notifyService,
24
                                EntityRepository $userRepository)
25
    {
26
        $this->dispatcher     = $dispatcher;
27
        $this->mailerService  = $mailerService;
28
        $this->notifyService  = $notifyService;
29
        $this->userRepository = $userRepository;
30
    }
31
32
    public function postPersist(Newsitem $newsitem)
33
    {
34
        $club = $newsitem->getAuthorClub();
35
        $text = substr($newsitem->getText(), 0, 140).'...';
36
37
        // Si ce n'est pas un message perso, on notifie les utilisateurs suivant le club
38
        if ($club) {
39
            $achievementCheck = new AchievementCheckEvent(Achievement::NEWS_CREATE);
40
            $this->dispatcher->dispatch('upont.achievement', $achievementCheck);
41
42
            list($usersPush, $usersMail) = $this->getUsersToNotify($club, $newsitem->getSendMail());
43
44
            $vars = ['post' => $newsitem];
45
46
            $attachments = [];
47 View Code Duplication
            foreach ($newsitem->getFiles() as $file) {
48
                $attachments[] = ['path' => $file->getAbsolutePath(), 'name' => $file->getName()];
49
            }
50
51
            $title = '['.$club->getName().']'.' '.$newsitem->getName();
52
            $this->mailerService->send($newsitem->getAuthorUser(),
0 ignored issues
show
Documentation introduced by
$newsitem->getAuthorUser() is of type object<KI\UserBundle\Entity\Club>, but the function expects a object<KI\UserBundle\Entity\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
                $usersMail,
54
                $title,
55
                'KIPublicationBundle::news.html.twig',
56
                $vars,
57
                $attachments
58
            );
59
60
            $text = substr($newsitem->getText(), 0, 140).'...';
61
            $this->notifyService->notify(
62
                'notif_followed_news',
63
                $newsitem->getName(),
64
                $text,
65
                'to',
66
                $usersPush
67
            );
68
        } else {
69
            // Si c'est une news perso on notifie tous ceux qui ont envie
70
            $this->notifyService->notify(
71
                'notif_news_perso',
72
                $newsitem->getName(),
73
                $text,
74
                'exclude',
75
                []
76
            );
77
        }
78
    }
79
80
    /**
81
     * Retourne les utilisateurs que l'on peut notifier par Push et/ou Mail
82
     * @param  Club  $club   Le club servant à déterminer l'égilibilité
83
     * @return array
84
     */
85
    private function getUsersToNotify(Club $club, $sendMail = false)
86
    {
87
        $allUsers = $this->userRepository->findAll();
88
        $usersPush = $usersMail = [];
89
90 View Code Duplication
        foreach ($allUsers as $candidate) {
91
            if (!$candidate->getClubsNotFollowed()->contains($club)) {
92
                $usersPush[] = $candidate;
93
94
                if ($sendMail && $candidate->getMailEvent()) {
95
                    $usersMail[] = $candidate;
96
                }
97
            }
98
        }
99
        return [$usersPush, $usersMail];
100
    }
101
}
102