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

ArticleContext::theFollowingArticles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 1
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 Doctrine\ORM\EntityManagerInterface;
10
use Faker\Provider\Lorem;
11
use Faker\Provider\Uuid;
12
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
13
use SWP\Bundle\ContentBundle\Factory\ArticleFactoryInterface;
14
use SWP\Bundle\ContentBundle\Factory\RouteFactoryInterface;
15
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
16
use SWP\Bundle\ContentBundle\Model\RouteInterface;
17
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
18
19
final class ArticleContext extends AbstractContext implements Context
20
{
21
    private $articleFactory;
22
23
    private $articleRepository;
24
25
    private $entityManager;
26
27
    private $routeFactory;
28
29
    private $routeRepository;
30
31
    public function __construct(
32
        ArticleFactoryInterface $articleFactory,
33
        ArticleRepositoryInterface $articleRepository,
34
        EntityManagerInterface $entityManager,
35
        RouteFactoryInterface $routeFactory,
36
        RouteRepositoryInterface $routeRepository
37
    ) {
38
        $this->articleFactory = $articleFactory;
39
        $this->articleRepository = $articleRepository;
40
        $this->entityManager = $entityManager;
41
        $this->routeFactory = $routeFactory;
42
        $this->routeRepository = $routeRepository;
43
    }
44
45
    /**
46
     * @Given the following Articles:
47
     */
48
    public function theFollowingArticles(TableNode $table)
49
    {
50
        foreach ($table as $row => $columns) {
51
            /** @var ArticleInterface $article */
52
            $article = $this->articleFactory->create();
53
            $article->setLocale('en');
54
            $article->setCode(Uuid::uuid());
55
56
            $columns['route'] = $this->getRoute($columns['route']);
57
            if (!isset($columns['body'])) {
58
                $columns['body'] = implode(' ', Lorem::paragraphs(3));
59
            }
60
61
            $this->fillObject($article, $columns);
62
            $this->entityManager->persist($article);
63
        }
64
65
        $this->entityManager->flush();
66
    }
67
68
    private function getRoute(string $routeName): RouteInterface
69
    {
70
        /** @var RouteInterface $route */
71
        $route = $this->routeRepository->findOneBy(['name' => $routeName]);
72
        if (null !== $route) {
73
            return $route;
74
        }
75
76
        /** @var RouteInterface $route */
77
        $route = $this->routeFactory->create();
78
        $route->setName($routeName);
79
        $route->setType(RouteInterface::TYPE_COLLECTION);
80
        $this->entityManager->persist($route);
81
        $this->entityManager->flush();
82
83
        return $route;
84
    }
85
}
86