summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/Branch/Branch.php
blob: c99b1bddbcdacb86dff19bd7fafba9565bc1a748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php

namespace Icinga\Module\Director\Db\Branch;

use Icinga\Application\Hook;
use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Hook\BranchSupportHook;
use Icinga\Web\Request;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use RuntimeException;
use stdClass;

/**
 * Knows whether we're in a branch
 */
class Branch
{
    const PREFIX_SYNC_PREVIEW = '/syncpreview';

    /** @var UuidInterface|null */
    protected $branchUuid;

    /** @var string */
    protected $name;

    /** @var string */
    protected $owner;

    /** @var @var string */
    protected $description;

    /** @var ?int */
    protected $tsMergeRequest;

    /** @var int */
    protected $cntActivities;

    public static function fromDbRow(stdClass $row)
    {
        $self = new static;
        if (is_resource($row->uuid)) {
            $row->uuid = stream_get_contents($row->uuid);
        }
        if (strlen($row->uuid) !== 16) {
            throw new RuntimeException('Valid UUID expected, got ' . var_export($row->uuid, true));
        }
        $self->branchUuid = Uuid::fromBytes(Db\DbUtil::binaryResult($row->uuid));
        $self->name = $row->branch_name;
        $self->owner = $row->owner;
        $self->description = $row->description;
        $self->tsMergeRequest = $row->ts_merge_request;
        if (isset($row->cnt_activities)) {
            $self->cntActivities = $row->cnt_activities;
        } else {
            $self->cntActivities = 0;
        }

        return $self;
    }

    /**
     * @return Branch
     */
    public static function detect(BranchStore $store)
    {
        try {
            return static::forRequest(Icinga::app()->getRequest(), $store, Auth::getInstance());
        } catch (\Exception $e) {
            return new static();
        }
    }

    /**
     * @param Request $request
     * @param Db $db
     * @param Auth $auth
     * @return Branch
     */
    public static function forRequest(Request $request, BranchStore $store, Auth $auth)
    {
        if ($hook = static::optionalHook()) {
            return $hook->getBranchForRequest($request, $store, $auth);
        }

        return new Branch;
    }

    /**
     * @return BranchSupportHook
     */
    public static function requireHook()
    {
        if ($hook = static::optionalHook()) {
            return $hook;
        }

        throw new RuntimeException('BranchSupport Hook requested where not available');
    }

    /**
     * @return BranchSupportHook|null
     */
    public static function optionalHook()
    {
        return Hook::first('director/BranchSupport');
    }

    /**
     * @param UuidInterface $uuid
     * @return Branch
     */
    public static function withUuid(UuidInterface $uuid)
    {
        $self = new static();
        $self->branchUuid = $uuid;
        return $self;
    }

    /**
     * @return bool
     */
    public function isBranch()
    {
        return $this->branchUuid !== null;
    }

    public function assertBranch()
    {
        if ($this->isMain()) {
            throw new RuntimeException('Branch expected, but working in main branch');
        }
    }

    /**
     * @return bool
     */
    public function isMain()
    {
        return $this->branchUuid === null;
    }

    /**
     * @return bool
     */
    public function shouldBeMerged()
    {
        return $this->tsMergeRequest !== null;
    }

    /**
     * @return bool
     */
    public function isEmpty()
    {
        return $this->cntActivities === 0;
    }

    /**
     * @return int
     */
    public function getActivityCount()
    {
        return $this->cntActivities;
    }

    /**
     * @return UuidInterface|null
     */
    public function getUuid()
    {
        return $this->branchUuid;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @since v1.10.0
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @since v1.10.0
     * @param ?string $description
     * @return void
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @return string
     */
    public function getOwner()
    {
        return $this->owner;
    }

    public function isSyncPreview()
    {
        return (bool) preg_match('/^' . preg_quote(self::PREFIX_SYNC_PREVIEW, '/') . '\//', $this->getName());
    }
}