Completed
Push — master ( 4e9843...e3ab56 )
by Jacob
03:59
created

MysqlChangelogRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Jacobemerick\Web\Domain\Stream\Changelog;
4
5
use Aura\Sql\ConnectionLocator;
6
use DateTime;
7
8
class MysqlChangelogRepository implements ChangelogRepositoryInterface
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 $limit
24
     * @param integer $offset
25
     *
26
     * @return array|false
27
     */
28 View Code Duplication
    public function getChanges($limit = null, $offset = 0)
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...
29
    {
30
        $query = "
31
            SELECT `message`, `message_short`, `datetime`, `commit_link`
32
            FROM `jpemeric_stream`.`changelog`
33
            ORDER BY `datetime` DESC";
34
        if (!is_null($limit)) {
35
          $query .= "
36
          LIMIT {$offset}, {$limit}";
37
        }
38
39
        return $this
40
            ->connections
41
            ->getRead()
42
            ->fetchAll($query);
43
    }
44
45
    /**
46
     * @param string $hash
47
     *
48
     * @return array|false
49
     */
50
    public function getChangeByHash($hash)
51
    {
52
        $query = "
53
            SELECT *
54
            FROM `jpemeric_stream`.`changelog`
55
            WHERE `hash` = :hash
56
            LIMIT 1";
57
58
        $bindings = [
59
            'hash' => $hash,
60
        ];
61
62
        return $this
63
            ->connections
64
            ->getRead()
65
            ->fetchOne($query, $bindings);
66
    }
67
68
    /**
69
     * @param string   $hash
70
     * @param string   $message
71
     * @param DateTime $datetime
72
     * @param string   $author
73
     * @param string   $commit_link
74
     *
75
     * @return
76
     */
77
    public function insertChange($hash, $message, DateTime $datetime, $author, $commit_link)
78
    {
79
        $message_short = $message;
80
        $message_short = strtok($message_short, "\n");
81
        if (strlen($message_short) > 72) {
82
            $message_short = wordwrap($message_short, 65);
83
            $message_short = strtok($message_short, "\n");
84
            $message_short .= '...';
85
        }
86
87
        $query = "
88
            INSERT INTO `jpemeric_stream`.`changelog`
89
                (`hash`, `message`, `message_short`, `datetime`, `author`, `commit_link`)
90
            VALUES
91
                (:hash, :message, :message_short, :datetime, :author, :commit_link)";
92
93
        $bindings = [
94
            'hash' => $hash,
95
            'message' => $message,
96
            'message_short' => $message_short,
97
            'datetime' => $datetime->format('Y-m-d H:i:s'),
98
            'author' => $author,
99
            'commit_link' => $commit_link
100
        ];
101
102
        return $this
103
            ->connections
104
            ->getWrite()
105
            ->perform($query, $bindings);
106
    }
107
}
108