Completed
Push — master ( 692aa1...25e5a7 )
by Paweł
60:22
created

ContentListService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addArticleToContentList() 0 22 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content List Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentListBundle\Services;
18
19
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
20
use SWP\Bundle\ContentListBundle\Event\ContentListEvent;
21
use SWP\Component\ContentList\ContentListEvents;
22
use SWP\Component\ContentList\Model\ContentListInterface;
23
use SWP\Component\ContentList\Model\ContentListItemInterface;
24
use SWP\Component\ContentList\Model\ListContentInterface;
25
use SWP\Component\Storage\Factory\FactoryInterface;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
28
final class ContentListService implements ContentListServiceInterface
29
{
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    protected $eventDispatcher;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    protected $listItemFactory;
39
40
    /**
41
     * ContentListService constructor.
42
     *
43
     * @param EventDispatcherInterface $eventDispatcher
44
     * @param FactoryInterface         $listItemFactory
45
     */
46
    public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $listItemFactory)
47
    {
48
        $this->eventDispatcher = $eventDispatcher;
49
        $this->listItemFactory = $listItemFactory;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function addArticleToContentList(ContentListInterface $contentList, ArticleInterface $article, $position = null)
56
    {
57
        /* @var ContentListItemInterface $contentListItem */
58
        $contentListItem = $this->listItemFactory->create();
59
60
        if ($article instanceof ListContentInterface) {
61
            $contentListItem->setContent($article);
62
        }
63
64
        if (null === $position) {
65
            $position = $contentList->getItems()->count();
66
        }
67
68
        $contentListItem->setPosition((int) $position);
69
        $contentList->addItem($contentListItem);
70
        $this->eventDispatcher->dispatch(
71
            ContentListEvents::POST_ITEM_ADD,
72
            new ContentListEvent($contentList, $contentListItem)
73
        );
74
75
        return $contentListItem;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $contentListItem; (SWP\Component\ContentLis...ontentListItemInterface) is incompatible with the return type declared by the interface SWP\Bundle\ContentListBu...addArticleToContentList of type SWP\Bundle\ContentListBu...ontentListItemInterface.

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...
76
    }
77
}
78