EventService   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 45
dl 0
loc 119
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getEventPages() 0 42 10
A getEventPagesArr() 0 9 2
A getEventMenuPages() 0 12 3
A __construct() 0 6 1
1
<?php
2
3
namespace Application\Bundle\DefaultBundle\Service;
4
5
use Application\Bundle\DefaultBundle\Repository\TicketCostRepository;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Stfalcon\Bundle\EventBundle\Entity\Event;
9
use Stfalcon\Bundle\EventBundle\Entity\EventGroup;
10
use Stfalcon\Bundle\EventBundle\Entity\EventPage;
11
use Stfalcon\Bundle\EventBundle\Repository\EventRepository;
12
use Stfalcon\Bundle\EventBundle\Repository\ReviewRepository;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
17
/**
18
 * Class EventService.
19
 */
20
class EventService
21
{
22
    /** @var EventRepository */
23
    private $eventRepository;
24
25
    /** @var TicketCostRepository */
26
    private $ticketCostRepository;
27
28
    /** @var ReviewRepository */
29
    private $reviewRepository;
30
31
    /** @var AuthorizationChecker */
32
    private $authorizationChecker;
33
34
    /**
35
     * EventService constructor.
36
     *
37
     * @param ObjectRepository              $eventRepository
38
     * @param ObjectRepository              $ticketCostRepository
39
     * @param ObjectRepository              $reviewRepository
40
     * @param AuthorizationCheckerInterface $authorizationChecker
41
     */
42
    public function __construct(ObjectRepository $eventRepository, ObjectRepository $ticketCostRepository, ObjectRepository $reviewRepository, AuthorizationCheckerInterface $authorizationChecker)
43
    {
44
        $this->eventRepository = $eventRepository;
0 ignored issues
show
Documentation Bug introduced by
$eventRepository is of type Doctrine\Common\Persistence\ObjectRepository, but the property $eventRepository was declared to be of type Stfalcon\Bundle\EventBun...ository\EventRepository. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
45
        $this->ticketCostRepository = $ticketCostRepository;
0 ignored issues
show
Documentation Bug introduced by
$ticketCostRepository is of type Doctrine\Common\Persistence\ObjectRepository, but the property $ticketCostRepository was declared to be of type Application\Bundle\Defau...ry\TicketCostRepository. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
46
        $this->reviewRepository = $reviewRepository;
0 ignored issues
show
Documentation Bug introduced by
$reviewRepository is of type Doctrine\Common\Persistence\ObjectRepository, but the property $reviewRepository was declared to be of type Stfalcon\Bundle\EventBun...sitory\ReviewRepository. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
        $this->authorizationChecker = $authorizationChecker;
0 ignored issues
show
Documentation Bug introduced by
$authorizationChecker is of type Symfony\Component\Securi...izationCheckerInterface, but the property $authorizationChecker was declared to be of type Symfony\Component\Securi...on\AuthorizationChecker. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
48
    }
49
50
    /**
51
     * @param Event $event
52
     * @param null  $reviewSlug
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $reviewSlug is correct as it would always require null to be passed?
Loading history...
53
     *
54
     * @return array
55
     */
56
    public function getEventPages(Event $event, $reviewSlug = null)
57
    {
58
        if ($event->isAdminOnly() && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
59
            throw new NotFoundHttpException(sprintf('Unable to find event by slug: %s', $event->getSlug()));
60
        }
61
        $review = null;
62
        if ($reviewSlug) {
0 ignored issues
show
introduced by
$reviewSlug is of type null, thus it always evaluated to false.
Loading history...
63
            $review = $this->reviewRepository->findOneBy(['slug' => $reviewSlug]);
64
65
            if (!$review) {
66
                throw new NotFoundHttpException('Unable to find Review entity.');
67
            }
68
        }
69
70
        /** @var ArrayCollection $pages */
71
        $pages = $this->getEventMenuPages($event);
72
73
        /** @var EventPage $page */
74
        $programPage = null;
75
        $venuePage = null;
76
        foreach ($pages as $key => $page) {
77
            if ('program' === $page->getSlug()) {
78
                $programPage = $page;
79
                unset($pages[$key]);
80
            } elseif ('venue' === $page->getSlug()) {
81
                $venuePage = $page;
82
                unset($pages[$key]);
83
            }
84
        }
85
86
        $eventCurrentAmount = $this->ticketCostRepository->getEventCurrentCost($event);
87
88
        $futureEvent = !$event->isActiveAndFuture() && $event->getGroup() instanceof EventGroup ? $this->eventRepository->findFutureEventFromSameGroup($event->getGroup()) : null;
89
90
        return [
91
            'event' => $event,
92
            'programPage' => $programPage,
93
            'venuePage' => $venuePage,
94
            'pages' => $pages,
95
            'review' => $review,
96
            'eventCurrentAmount' => $eventCurrentAmount,
97
            'futureEvent' => $futureEvent,
98
        ];
99
    }
100
101
    /**
102
     * Return array of event with pages.
103
     *
104
     * @param string      $eventSlug
105
     * @param string|null $reviewSlug
106
     *
107
     * @return array
108
     */
109
    public function getEventPagesArr($eventSlug, $reviewSlug = null)
110
    {
111
        /** @var Event $event */
112
        $event = $this->eventRepository->findOneBy(['slug' => $eventSlug]);
113
        if (!$event instanceof Event) {
0 ignored issues
show
introduced by
$event is always a sub-type of Stfalcon\Bundle\EventBundle\Entity\Event.
Loading history...
114
            throw new NotFoundHttpException(sprintf('Unable to find event by slug: %s', $eventSlug));
115
        }
116
117
        return $this->getEventPages($event, $reviewSlug);
0 ignored issues
show
Bug introduced by
It seems like $reviewSlug can also be of type string; however, parameter $reviewSlug of Application\Bundle\Defau...ervice::getEventPages() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
        return $this->getEventPages($event, /** @scrutinizer ignore-type */ $reviewSlug);
Loading history...
118
    }
119
120
    /**
121
     * Get event pages that may show.
122
     *
123
     * @param Event $event
124
     *
125
     * @return array
126
     */
127
    public function getEventMenuPages(Event $event)
128
    {
129
        /** TODO замінити на репозіторій */
130
        $pages = [];
131
        /** @var EventPage $page */
132
        foreach ($event->getPages() as $page) {
133
            if ($page->isShowInMenu()) {
134
                $pages[] = $page;
135
            }
136
        }
137
138
        return $pages;
139
    }
140
}
141