Test Failed
Push — master ( dc68d1...1f5199 )
by Mathieu
02:31
created

EventManagerAwareTrait::featEvents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 0
1
<?php
2
3
namespace Charcoal\Cms\Support\Traits;
4
5
use Charcoal\Cms\EventInterface;
6
use Charcoal\Cms\Service\Manager\EventManager;
7
use Charcoal\Object\CategoryInterface;
8
use Slim\Exception\ContainerException;
9
10
trait EventManagerAwareTrait
11
{
12
    /**
13
     * Currently displayed event.
14
     * @var array $event City/Object/Event
15
     */
16
    private $currentEvent;
17
18
    /**
19
     * @var EventManager $eventManager
20
     */
21
    private $eventManager;
22
23
    // ==========================================================================
24
    // FUNCTIONS
25
    // ==========================================================================
26
27
    /**
28
     * Formatted event list
29
     * Return the entries for the current page
30
     * @return \Generator|void
31
     */
32
    public function eventsList()
33
    {
34
        $eventList = $this->eventManager()->entries();
35
        foreach ($eventList as $event) {
36
            yield $this->eventFormatShort($event);
37
        }
38
    }
39
40
    /**
41
     * Formatted event archive list
42
     * Returns the entries for the current page.
43
     * @return \Generator|void
44
     */
45
    public function eventArchiveList()
46
    {
47
        $entries = $this->eventManager()->archive();
48
        foreach ($entries as $entry) {
49
            yield $this->eventFormatShort($entry);
50
        }
51
    }
52
53
    /**
54
     * Current event.
55
     * @return array The properties of the current event.
56
     */
57
    public function currentEvent()
58
    {
59
        if ($this->currentEvent) {
60
            return $this->currentEvent;
61
        }
62
63
        // The the current event
64
        $event = $this->eventManager()->entry();
65
66
        // Format the event
67
        if ($event) {
68
            $this->currentEvent = $this->eventFormatFull($event);
69
        }
70
71
        return $this->currentEvent;
72
    }
73
74
    /**
75
     * @return mixed
76
     */
77
    public function featEvents()
78
    {
79
        $entries = $this->eventManager()->featList();
80
81
        if (!$entries) {
82
            return;
83
        }
84
85
        foreach ($entries as $entry) {
86
            if ($entry->id()) {
87
                yield $this->eventFormatFull($entry);
88
            }
89
        }
90
    }
91
92
    /**
93
     * Next event in list.
94
     * @return array The next event properties.
95
     */
96
    public function nextEvent()
97
    {
98
        $next = $this->eventManager()->next();
99
100
        if (!$next) {
101
            return null;
102
        }
103
104
        return $this->eventFormatNav($next);
105
    }
106
107
    /**
108
     * Next event in list.
109
     * @return array The next event properties.
110
     */
111
    public function prevEvent()
112
    {
113
        $prev = $this->eventManager()->prev();
114
115
        if (!$prev) {
116
            return null;
117
        }
118
119
        return $this->eventFormatNav($prev);
120
    }
121
122
    /**
123
     * Amount of events (total)
124
     * @return integer How many events?
125
     */
126
    public function numEvent()
127
    {
128
        return $this->eventManager()->numEvent();
129
    }
130
131
    /**
132
     * @return float
133
     */
134
    public function numEventPages()
135
    {
136
        return $this->eventManager()->numPages();
137
    }
138
139
    /**
140
     * @return boolean
141
     */
142
    public function eventHasPager()
143
    {
144
        return $this->eventManager()->hasPager();
145
    }
146
147
    /**
148
     * @return \Generator
149
     */
150
    public function eventCategoryList()
151
    {
152
        $cats = $this->eventManager()->categoryItems();
153
        foreach ($cats as $cat) {
154
            yield $this->eventFormatCategory($cat);
155
        }
156
    }
157
158
    /**
159
     * @param EventInterface $event Charcoal\Cms\EventInterface.
160
     * @return CategoryInterface
161
     */
162
    protected function eventCategory(EventInterface $event)
163
    {
164
        $id = $event->category();
165
166
        return $this->eventManager()->categoryItem($id);
167
    }
168
169
    // ==========================================================================
170
    // FORMATTER
171
    // ==========================================================================
172
173
    /**
174
     * @param EventInterface $event Charcoal\Cms\EventInterface.
175
     * @return string
176
     */
177
    protected function getEventStartDateFormat(EventInterface $event)
178
    {
179
        return $this->dateHelper()->formatDate(
180
            $event->startDate()
181
        );
182
    }
183
184
    /**
185
     * @param EventInterface $event Charcoal\Cms\EventInterface.
186
     * @return string
187
     */
188
    protected function getEventEndDateFormat(EventInterface $event)
189
    {
190
        return $this->dateHelper()->formatDate(
191
            $event->endDate()
192
        );
193
    }
194
195
    /**
196
     * @param EventInterface $event Charcoal\Cms\EventInterface.
197
     * @return string
198
     */
199
    protected function getEventDateFormat(EventInterface $event)
200
    {
201
        return $this->dateHelper()->formatDate([
202
            $event->startDate(),
203
            $event->endDate()
204
        ]);
205
    }
206
207
    /**
208
     * @param EventInterface $event Charcoal\Cms\EventInterface.
209
     * @return string
210
     */
211
    protected function getEventTimeFormat(EventInterface $event)
212
    {
213
        if ($event->dateNotes() != '') {
0 ignored issues
show
Bug introduced by
The method dateNotes() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214
            return $event->dateNotes();
0 ignored issues
show
Bug introduced by
The method dateNotes() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
        }
216
217
        if (!$event->displayHours()) {
0 ignored issues
show
Bug introduced by
The method displayHours() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
            return null;
219
        }
220
221
        return $this->dateHelper()->formatTime([
222
            $event->startDate(),
223
            $event->endDate(),
224
        ]);
225
    }
226
227
    /**
228
     * Formatting expected in templates
229
     * @param EventInterface $event Charcoal\Cms\EventInterface.
230
     * @return array The needed event properties.
231
     */
232
    protected function eventFormatShort(EventInterface $event)
233
    {
234
        return [
235
            'title'     => (string)$event->title(),
236
            'url'       => (string)$event->url(),
237
            'startDate' => $this->getEventStartDateFormat($event),
238
            'endDate'   => $this->getEventEndDateFormat($event),
239
            'date'      => $this->getEventDateFormat($event),
240
            'time'      => $this->getEventTimeFormat($event),
241
            'active'    => ($this->currentEvent() && ($this->currentEvent()['id'] == $event->id()))
242
        ];
243
    }
244
245
    /**
246
     * Formatting expected in templates
247
     * @param EventInterface $event Charcoal\Cms\EventInterface.
248
     * @return array The needed event properties.
249
     */
250
    protected function eventFormatNav(EventInterface $event)
251
    {
252
        return [
253
            'startDate' => $this->getEventStartDateFormat($event),
254
            'endDate'   => $this->getEventEndDateFormat($event),
255
            'date'      => $this->getEventDateFormat($event),
256
            'time'      => $this->getEventTimeFormat($event),
257
            'title'     => (string)$event->title(),
258
            'url'       => $event->url()
259
        ];
260
    }
261
262
    /**
263
     * @param EventInterface $event The current event.
264
     * @return array The needed properties.
265
     */
266
    protected function eventFormatFull(EventInterface $event)
267
    {
268
        $contentBlocks = $event->attachments('content');
0 ignored issues
show
Bug introduced by
The method attachments() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
269
        $gallery = $event->attachments('image-gallery');
0 ignored issues
show
Bug introduced by
The method attachments() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
270
        $documents = $event->attachments('document');
0 ignored issues
show
Bug introduced by
The method attachments() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
271
272
        return [
273
            'id'               => $event->id(),
274
            'title'            => (string)$event->title(),
275
            'summary'          => (string)$event->summary(),
276
            'content'          => (string)$event->content(),
277
            'image'            => $event->image(),
278
            'startDate'        => $this->getEventStartDateFormat($event),
279
            'endDate'          => $this->getEventEndDateFormat($event),
280
            'date'             => $this->getEventDateFormat($event),
281
            'time'             => $this->getEventTimeFormat($event),
282
            'contentBlocks'    => $contentBlocks,
283
            'hasContentBlocks' => !!(count($contentBlocks)),
284
            'documents'        => $documents,
285
            'hasDocuments'     => !!(count($documents)),
286
            'gallery'          => $gallery,
287
            'hasGallery'       => !!(count($gallery)),
288
            'url'              => $event->url(),
289
            'metaTitle'        => (string)$event->metaTitle(),
290
            'locationName'     => (string)$event->locationName(),
0 ignored issues
show
Bug introduced by
The method locationName() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
291
            'dateNotes'        => (string)$event->dateNotes(),
0 ignored issues
show
Bug introduced by
The method dateNotes() does not seem to exist on object<Charcoal\Cms\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
292
            'category'         => $event->category()
293
        ];
294
    }
295
296
    /**
297
     * @param CategoryInterface $category The category item.
298
     * @return array The formatted category item.
299
     */
300
    protected function eventFormatCategory(CategoryInterface $category)
301
    {
302
        return [
303
            'id'   => $category->id(),
304
            'name' => (string)$category->name(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Charcoal\Object\CategoryInterface as the method name() does only exist in the following implementations of said interface: Charcoal\Attachment\Object\Category\Generic, Charcoal\Cms\NewsCategory, Charcoal\Cms\Tag.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
305
        ];
306
    }
307
308
    // ==========================================================================
309
    // DEPENDENCIES
310
    // ==========================================================================
311
312
    /**
313
     * @return EventManager
314
     * @throws ContainerException When dependency is missing.
315
     */
316
    protected function eventManager()
317
    {
318
        if (!$this->eventManager instanceof EventManager) {
319
            throw new ContainerException(sprintf(
320
                'Missing dependency for %s: %s',
321
                get_called_class(),
322
                EventManager::class
323
            ));
324
        }
325
326
        return $this->eventManager;
327
    }
328
329
    /**
330
     * @param EventManager $eventManager The event Manager class.
331
     * @return self
332
     */
333
    protected function setEventManager(EventManager $eventManager)
334
    {
335
        $this->eventManager = $eventManager;
336
337
        return $this;
338
    }
339
340
    /**
341
     * dateHelperAwareTrait dependency
342
     * @return mixed
343
     */
344
    abstract protected function dateHelper();
345
}
346