|
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; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.