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

ArticleContext::getArticleTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 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