PostRepository::findBySlug()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Core\Component\Blog\Application\Repository\DQL;
16
17
use Acme\App\Core\Component\Blog\Application\Repository\PostRepositoryInterface;
18
use Acme\App\Core\Component\Blog\Domain\Post\Post;
19
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
20
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
21
use Acme\App\Core\Port\Persistence\PersistenceServiceInterface;
22
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
23
24
/**
25
 * This custom Doctrine repository contains some methods which are useful when
26
 * querying for blog post information.
27
 *
28
 * See https://symfony.com/doc/current/doctrine/repository.html
29
 *
30
 * @author Ryan Weaver <[email protected]>
31
 * @author Javier Eguiluz <[email protected]>
32
 * @author Yonel Ceruto <[email protected]>
33
 * @author Herberto Graca <[email protected]>
34
 */
35
class PostRepository implements PostRepositoryInterface
36
{
37
    /**
38
     * @var DqlQueryBuilderInterface
39
     */
40
    private $dqlQueryBuilder;
41
42
    /**
43
     * @var QueryServiceRouterInterface
44
     */
45
    private $queryService;
46
47
    /**
48
     * @var PersistenceServiceInterface
49
     */
50
    private $persistenceService;
51
52
    public function __construct(
53
        DqlQueryBuilderInterface $dqlQueryBuilder,
54
        QueryServiceRouterInterface $queryService,
55
        PersistenceServiceInterface $persistenceService
56
    ) {
57
        $this->dqlQueryBuilder = $dqlQueryBuilder;
58
        $this->queryService = $queryService;
59
        $this->persistenceService = $persistenceService;
60
    }
61
62
    public function find(PostId $id): Post
63
    {
64
        $dqlQuery = $this->dqlQueryBuilder->create(Post::class)
65
            ->where('Post.id = :id')
66
            ->setParameter('id', $id)
67
            ->build();
68
69
        return $this->queryService->query($dqlQuery)->getSingleResult();
70
    }
71
72 View Code Duplication
    public function findBySlug(string $slug): Post
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $dqlQuery = $this->dqlQueryBuilder->create(Post::class)
75
            ->where('Post.slug = :slug')
76
            ->setParameter('slug', $slug)
77
            ->build();
78
79
        return $this->queryService->query($dqlQuery)->getSingleResult();
80
    }
81
82
    public function add(Post $entity): void
83
    {
84
        $this->persistenceService->upsert($entity);
85
    }
86
87
    public function remove(Post $entity): void
88
    {
89
        // Delete the tags associated with this blog post. This is done automatically
90
        // by Doctrine, except for SQLite (the database used in this application)
91
        // because foreign key support is not enabled by default in SQLite
92
        $entity->clearTags();
93
94
        $this->persistenceService->delete($entity);
95
    }
96
}
97