Completed
Push — master ( a8a7ef...2a1bc1 )
by Jacob
15:24
created

MysqlBlogRepository::insertBlog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 19
rs 9.4286
c 1
b 0
f 1
cc 1
eloc 10
nc 1
nop 3
1
<?php
2
3
namespace Jacobemerick\Web\Domain\Stream\Blog;
4
5
use Aura\Sql\ConnectionLocator;
6
use DateTime;
7
8
class MysqlBlogRepository implements BlogRepositoryInterface
9
{
10
11
    /** @var  ConnectionLocator */
12
    protected $connections;
13
14
    /**
15
     * @param ConnectonLocator $connections
16
     */
17
    public function __construct(ConnectionLocator $connections)
18
    {
19
        $this->connections = $connections;
20
    }
21
22
    /**
23
     * @param integer $id
24
     *
25
     * @return array|false
26
     */
27
    public function getBlogById($id)
28
    {
29
        $query = "
30
            SELECT *
31
            FROM `jpemeric_stream`.`blog`
32
            WHERE `id` = :id
33
            LIMIT 1";
34
        $bindings = [
35
            'id' => $id,
36
        ];
37
38
        return $this
39
            ->connections
40
            ->getRead()
41
            ->fetchOne($query, $bindings);
42
    }
43
44 View Code Duplication
    public function getBlogByPermalink($permalink)
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...
45
    {
46
        $query = "
47
            SELECT *
48
            FROM `jpemeric_stream`.`blog2`
49
            WHERE `permalink` = :permalink
50
            LIMIT 1";
51
        $bindings = [
52
            'permalink' => $permalink,
53
        ];
54
55
        return $this
56
            ->connections
57
            ->getRead()
58
            ->fetchOne($query, $bindings);
59
    }
60
61
    /**
62
     * @param string $title
63
     *
64
     * @return array|false
65
     */
66
    public function getBlogByTitle($title)
67
    {
68
        $query = "
69
            SELECT *
70
            FROM `jpemeric_stream`.`blog`
71
            WHERE `title` = :title
72
            LIMIT 1";
73
        $bindings = [
74
            'title' => $title,
75
        ];
76
77
        return $this
78
            ->connections
79
            ->getRead()
80
            ->fetchOne($query, $bindings);
81
    }
82
83
    public function getBlogs($limit = null, $offset = 0)
84
    {
85
        $query = "
86
            SELECT `id`, `permalink`, `datetime`
87
            FROM `jpemeric_stream`.`blog2`
88
            ORDER BY `datetime` DESC";
89
        if (!is_null($limit)) {
90
            $query .= "
91
            LIMIT {$limit}, {$offset}";
92
        }
93
94
        return $this
95
            ->connections
96
            ->getRead()
97
            ->fetchAll($query);
98
    }
99
100
    public function insertBlog($permalink, DateTime $datetime, array $metadata)
101
    {
102
        $query = "
103
            INSERT INTO `jpemeric_stream`.`blog2`
104
                (`permalink`, `datetime`, `metadata`)
105
            VALUES
106
                (:permalink, :datetime, :metadata)";
107
108
        $bindings = [
109
            'permalink' => $permalink,
110
            'datetime' => $datetime->format('Y-m-d H:i:s'),
111
            'metadata' => json_encode($metadata),
112
        ];
113
114
        return $this
115
            ->connections
116
            ->getWrite()
117
            ->perform($query, $bindings);
118
    }
119
}
120