Completed
Push — master ( 82f14f...636df4 )
by Kristof
10s
created

LocalEventService::eventsOrganizedByOrganizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3;
7
8
use Broadway\Repository\RepositoryInterface;
9
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
10
use CultuurNet\UDB3\Event\ReadModel\Relations\RepositoryInterface as RelationsRepository;
11
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
12
13
class LocalEventService extends LocalEntityService implements EventServiceInterface
14
{
15
    /**
16
     * @var Event\ReadModel\Relations\RepositoryInterface
17
     */
18
    protected $eventRelationsRepository;
19
20
    public function __construct(
21
        DocumentRepositoryInterface $documentRepository,
22
        RepositoryInterface $eventRepository,
23
        RelationsRepository $eventRelationsRepository,
24
        IriGeneratorInterface $iriGenerator
25
    ) {
26
        parent::__construct($documentRepository, $eventRepository, $iriGenerator);
27
        $this->eventRelationsRepository = $eventRelationsRepository;
28
    }
29
30
    /**
31
     * Get a single event by its id.
32
     *
33
     * @obsolete
34
     *   Use getEntity() instead.
35
     *
36
     * @param string $id
37
     *   A string uniquely identifying an event.
38
     *
39
     * @return array
40
     *   An event array.
41
     *
42
     * @throws EventNotFoundException if an event can not be found for the given id
43
     */
44
    public function getEvent($id)
45
    {
46
        try {
47
            return $this->getEntity($id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntity($id); (string) is incompatible with the return type declared by the interface CultuurNet\UDB3\EventServiceInterface::getEvent of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
        } catch (EntityNotFoundException $e) {
49
            throw new EventNotFoundException(
50
                "Event with id: {$id} not found"
51
            );
52
        }
53
    }
54
55
    /**
56
     * @param string $organizerId
57
     * @return string[]
58
     */
59
    public function eventsOrganizedByOrganizer($organizerId)
60
    {
61
        return $this->eventRelationsRepository->getEventsOrganizedByOrganizer($organizerId);
62
    }
63
64
    /**
65
     * @param string $placeId
66
     * @return string[]
67
     */
68
    public function eventsLocatedAtPlace($placeId)
69
    {
70
        return $this->eventRelationsRepository->getEventsLocatedAtPlace($placeId);
71
    }
72
}
73