Completed
Pull Request — 1.5 (#758)
by Paweł
11:13
created

ContentListItemContext   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A theFollowingContentListItems() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Behat\Contexts;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Gherkin\Node\TableNode;
9
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
10
use SWP\Component\ContentList\Model\ContentListItemInterface;
11
use SWP\Component\ContentList\Repository\ContentListRepositoryInterface;
12
use SWP\Component\Storage\Factory\FactoryInterface;
13
14
final class ContentListItemContext extends AbstractContext implements Context
15
{
16
    private $contentListItemFactory;
17
18
    private $contentListRepository;
19
20
    private $articleRepository;
21
22
    public function __construct(
23
        FactoryInterface $contentListItemFactory,
24
        ContentListRepositoryInterface $contentListRepository,
25
        ArticleRepositoryInterface $articleRepository
26
    ) {
27
        $this->contentListItemFactory = $contentListItemFactory;
28
        $this->contentListRepository = $contentListRepository;
29
        $this->articleRepository = $articleRepository;
30
    }
31
32
    /**
33
     * @Given the following Content List Items:
34
     */
35
    public function theFollowingContentListItems(TableNode $table)
36
    {
37
        foreach ($table as $row => $columns) {
38
            /** @var ContentListItemInterface $contentListItem */
39
            $contentListItem = $this->contentListItemFactory->create();
40
            $contentListItem->setPosition(0);
41
42
            $columns['content_list'] = $this->contentListRepository->findOneBy(['name' => $columns['content_list']]);
43
            $columns['content'] = $this->articleRepository->findOneBy(['title' => $columns['article']]);
44
            unset($columns['article']);
45
46
            $this->fillObject($contentListItem, $columns);
47
            $this->contentListRepository->persist($contentListItem);
48
            $this->contentListRepository->flush();
49
        }
50
    }
51
}
52