Completed
Pull Request — example/managing-articles (#87)
by Loïc
03:50
created

ArticleContext   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getArticleTitle() 0 12 1
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Behat\Context\Transform;
15
16
use App\Entity\Article;
17
use Behat\Behat\Context\Context;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
use Webmozart\Assert\Assert;
20
21
class ArticleContext implements Context
22
{
23
    /**
24
     * @var RepositoryInterface
25
     */
26
    private $articleRepository;
27
28
    /**
29
     * ArticleContext constructor.
30
     *
31
     * @param RepositoryInterface $articleRepository
32
     */
33
    public function __construct(RepositoryInterface $articleRepository)
34
    {
35
        $this->articleRepository = $articleRepository;
36
    }
37
38
    /**
39
     * @Transform /^article "([^"]+)"$/
40
     * @Transform :article
41
     *
42
     * @param string $title
43
     *
44
     * @return Article
45
     */
46
    public function getArticleTitle($title)
47
    {
48
        /** @var Article $article */
49
        $article = $this->articleRepository->findOneBy(['title' => $title]);
50
51
        Assert::notNull(
52
            $article,
53
            sprintf('Article with title "%s" does not exist', $title)
54
        );
55
56
        return $article;
57
    }
58
}
59