Passed
Push — main ( d581be...e16750 )
by Diego
02:53
created

Projects::addWikiPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Blackmine\Repository\Projects;
6
7
use Blackmine\Exception\Api\AbstractApiException;
8
use Blackmine\Exception\InvalidModelException;
9
use Blackmine\Model\News;
10
use Blackmine\Model\Project\File;
11
use Blackmine\Model\Project\IssueCategory;
12
use Blackmine\Model\Project\Module;
13
use Blackmine\Model\Project\Project;
14
use Blackmine\Model\Project\TimeEntry;
15
use Blackmine\Model\Project\Tracker;
16
use Blackmine\Model\Project\Version;
17
use Blackmine\Model\Project\WikiPage;
18
use Blackmine\Model\User\Membership;
19
use Blackmine\Repository\AbstractRepository;
20
use Blackmine\Repository\Uploads;
21
use Doctrine\Common\Collections\ArrayCollection;
22
use Doctrine\Common\Collections\Collection;
23
use JsonException;
24
25
class Projects extends AbstractRepository
26
{
27
    public const API_ROOT = "projects";
28
29
    public const PROJECT_RELATION_TRACKERS = "trackers";
30
    public const PROJECT_RELATION_ISSUE_CATEGORIES = "issue_categories";
31
    public const PROJECT_RELATION_ENABLED_MODULES = "enabled_modules";
32
    public const PROJECT_RELATION_TIME_ENTRIES = "time_entries";
33
    public const PROJECT_RELATION_MEMBERSHIPS = "memberships";
34
    public const PROJECT_RELATION_VERSIONS = "versions";
35
    public const PROJECT_RELATION_FILES = "files";
36
    public const PROJECT_RELATION_NEWS = "news";
37
    public const PROJECT_RELATION_WIKI_PAGES = "wiki_pages";
38
39
    protected static array $relation_class_map = [
40
        self::PROJECT_RELATION_TRACKERS => Tracker::class,
41
        self::PROJECT_RELATION_ISSUE_CATEGORIES => IssueCategory::class,
42
        self::PROJECT_RELATION_ENABLED_MODULES => Module::class,
43
        self::PROJECT_RELATION_TIME_ENTRIES => TimeEntry::class,
44
        self::PROJECT_RELATION_MEMBERSHIPS => Membership::class,
45
        self::PROJECT_RELATION_VERSIONS => Version::class,
46
        self::PROJECT_RELATION_FILES => File::class,
47
        self::PROJECT_RELATION_NEWS => News::class,
48
        self::PROJECT_RELATION_WIKI_PAGES => WikiPage::class
49
    ];
50
51
    protected static array $allowed_filters = [];
52
53
    public function getModelClass(): string
54
    {
55
        return Project::class;
56
    }
57
58
    public function getTimeEntries(Project $project): ArrayCollection
59
    {
60
        return $this->client->getRepository(TimeEntries::API_ROOT)
61
            ->addFilter(TimeEntries::TIME_ENTRY_FILTER_PROJECT_ID, $project->getId())
62
            ->search();
63
    }
64
65
    /**
66
     * @throws AbstractApiException
67
     * @throws InvalidModelException
68
     * @throws JsonException
69
     */
70
    public function addTimeEntry(Project $project, TimeEntry $time_entry): Project
71
    {
72
        $time_entry->setProject($project);
73
        $this->client->getRepository(TimeEntries::API_ROOT)?->create($time_entry);
74
75
        return $project;
76
    }
77
78
    /**
79
     * @throws AbstractApiException
80
     * @throws JsonException
81
     */
82
    public function getWikiPages(Project $project): Collection
83
    {
84
        /** @var WikiPages $wiki_pages */
85
        $wiki_pages = $this->client->getRepository(WikiPages::API_ROOT);
86
        $wiki_pages->setProject($project);
87
88
        return $wiki_pages->all();
89
    }
90
91
    /**
92
     * @throws AbstractApiException
93
     * @throws InvalidModelException
94
     * @throws JsonException
95
     */
96
    public function addFile(Project $project, File $file): Project
97
    {
98
        $file = $this->client->getRepository(Uploads::API_ROOT)?->create($file);
99
        /** @var File $file */
100
        if ($file !== null) {
101
            $file->setVersion($project->getDefaultVersion());
102
103
            $endpoint = $this->getEndpoint() . "/" . $project->getId() . "/files." . $this->client->getFormat();
104
            $api_response = $this->client->post($endpoint, json_encode($file->getPayload(), JSON_THROW_ON_ERROR));
105
106
            if ($api_response->isSuccess()) {
107
                $project->addFile($file);
108
            } else {
109
                throw AbstractApiException::fromApiResponse($api_response);
110
            }
111
        }
112
113
        return $project;
114
    }
115
116
    /**
117
     * @throws AbstractApiException
118
     * @throws JsonException
119
     */
120
    public function archive(Project $project): Project
121
    {
122
        $endpoint = $this->getEndpoint() . "/" . $project->getId() . "/archive" . "." . $this->client->getFormat();
123
        $api_response = $this->client->put($endpoint, '', ["Content-Length" => 0]);
124
125
        if (!$api_response->isSuccess()) {
126
            throw AbstractApiException::fromApiResponse($api_response);
127
        }
128
129
        return $project;
130
    }
131
132
    /**
133
     * @throws AbstractApiException
134
     * @throws JsonException
135
     */
136
    public function unArchive(Project $project): Project
137
    {
138
        $endpoint = $this->getEndpoint() . "/" . $project->getId() . "/unarchive" . "." . $this->client->getFormat();
139
        $api_response = $this->client->put($endpoint, '', ["Content-Length" => 0]);
140
141
        if (!$api_response->isSuccess()) {
142
            throw AbstractApiException::fromApiResponse($api_response);
143
        }
144
145
        return $project;
146
    }
147
}
148