Passed
Push — main ( 5879cf...b2c9c8 )
by Diego
03:34
created

Projects::getWikiPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 10
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
    public function addWikiPage(Project $project, WikiPage $wiki_page): Project
0 ignored issues
show
Unused Code introduced by
The parameter $wiki_page is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

91
    public function addWikiPage(Project $project, /** @scrutinizer ignore-unused */ WikiPage $wiki_page): Project

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        return $project;
94
    }
95
96
    /**
97
     * @throws AbstractApiException
98
     * @throws InvalidModelException
99
     * @throws JsonException
100
     */
101
    public function addFile(Project $project, File $file): Project
102
    {
103
        $file = $this->client->getRepository(Uploads::API_ROOT)?->create($file);
104
        /** @var File $file */
105
        if ($file !== null) {
106
            $file->setVersion($project->getDefaultVersion());
107
108
            $endpoint = $this->getEndpoint() . "/" . $project->getId() . "/files." . $this->client->getFormat();
109
            $api_response = $this->client->post($endpoint, json_encode($file->getPayload(), JSON_THROW_ON_ERROR));
110
111
            if ($api_response->isSuccess()) {
112
                $project->addFile($file);
113
            } else {
114
                throw AbstractApiException::fromApiResponse($api_response);
115
            }
116
        }
117
118
        return $project;
119
    }
120
121
    /**
122
     * @throws AbstractApiException
123
     * @throws JsonException
124
     */
125
    public function archive(Project $project): Project
126
    {
127
        $endpoint = $this->getEndpoint() . "/" . $project->getId() . "/archive" . "." . $this->client->getFormat();
128
        $api_response = $this->client->put($endpoint, '', ["Content-Length" => 0]);
129
130
        if (!$api_response->isSuccess()) {
131
            throw AbstractApiException::fromApiResponse($api_response);
132
        }
133
134
        return $project;
135
    }
136
137
    /**
138
     * @throws AbstractApiException
139
     * @throws JsonException
140
     */
141
    public function unArchive(Project $project): Project
142
    {
143
        $endpoint = $this->getEndpoint() . "/" . $project->getId() . "/unarchive" . "." . $this->client->getFormat();
144
        $api_response = $this->client->put($endpoint, '', ["Content-Length" => 0]);
145
146
        if (!$api_response->isSuccess()) {
147
            throw AbstractApiException::fromApiResponse($api_response);
148
        }
149
150
        return $project;
151
    }
152
}
153