Completed
Push — master ( c330e2...cf0c7a )
by Jacob
03:58
created

MysqlBookRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
lcom 1
cbo 2
dl 77
loc 77
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getBookById() 16 16 1
A getBookByFields() 17 17 1
A getUnmappedBooks() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Jacobemerick\Web\Domain\Stream\Book;
4
5
use Aura\Sql\ConnectionLocator;
6
7 View Code Duplication
class MysqlBookRepository implements BookRepositoryInterface
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
10
    /** @var  ConnectionLocator */
11
    protected $connections;
12
13
    /**
14
     * @param ConnectonLocator $connections
15
     */
16
    public function __construct(ConnectionLocator $connections)
17
    {
18
        $this->connections = $connections;
19
    }
20
21
    /**
22
     * @param integer $id
23
     *
24
     * @return array|false
25
     */
26
    public function getBookById($id)
27
    {
28
        $query = "
29
            SELECT `id`, `title`, `author`, `image`, `date_read` AS `date`
30
            FROM `jpemeric_stream`.`book`
31
            WHERE `id` = :id
32
            LIMIT 1";
33
        $bindings = [
34
            'id' => $id,
35
        ];
36
37
        return $this
38
            ->connections
39
            ->getRead()
40
            ->fetchOne($query, $bindings);
41
    }
42
43
    /**
44
     * @param string $title
45
     * @param string $author
46
     *
47
     * @return array|false
48
     */
49
    public function getBookByFields($title, $author)
50
    {
51
        $query = "
52
            SELECT *
53
            FROM `jpemeric_stream`.`book`
54
            WHERE `title` = :title AND `author` = :author
55
            LIMIT 1";
56
        $bindings = [
57
            'title' => $title,
58
            'author' => $author,
59
        ];
60
61
        return $this
62
            ->connections
63
            ->getRead()
64
            ->fetchOne($query, $bindings);
65
    }
66
67
    /**
68
     * @return array|false
69
     */
70
    public function getUnmappedBooks()
71
    {
72
        $query = "
73
            SELECT `id`, `date`
74
            FROM `jpemeric_stream`.`book`
75
            LEFT JOIN `jpemeric_stream`.`post`
76
            ON `post`.`type_id` = `book`.`id` AND `post`.`id` IS NULL";
77
78
        return $this
79
            ->connections
80
            ->getRead()
81
            ->fetchAll($query);
82
    }
83
}
84