Test Failed
Branch feature/v1_stable_fixes (e805e7)
by Diego
04:22
created

Uploads   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 82
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelClass() 0 3 1
A create() 0 33 5
A get() 0 4 1
A update() 0 4 1
A delete() 0 4 1
A all() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Blackmine\Repository;
6
7
use Blackmine\Exception\Api\AbstractApiException;
8
use Blackmine\Exception\InvalidModelException;
9
use Blackmine\Exception\MethodNotImplementedException;
10
use Blackmine\Model\AbstractModel;
11
use Blackmine\Model\Project\File;
12
use Blackmine\Model\Upload;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Error;
15
16
class Uploads extends AbstractRepository
17
{
18
19
    public const API_ROOT = "uploads";
20
21
    public function getModelClass(): string
22
    {
23
        return Upload::class;
24
    }
25
26
    public function create(AbstractModel $model): ?AbstractModel
27
    {
28
        if (!$model instanceof File) {
29
            throw new InvalidModelException(
30
                'Wrong model class for ' . $this->getEndpoint() . " api. Expected " . File::class
31
            );
32
        }
33
34
        $file = $model->getFilename();
35
36
        if (file_exists($file)) {
37
            $filename = basename($file);
38
39
            $api_response = $this->client->post(
40
                $this->getEndpoint() . "." . $this->client->getFormat() . "?filename=" . $filename,
41
                file_get_contents($file),
42
                ["Content-Type" => "application/octet-stream"]
43
            );
44
45
            if ($api_response->isSuccess()) {
46
                $model_data = $api_response->getData()["upload"] ?? null;
47
                if ($model_data) {
48
                    $model->fromArray($model_data);
49
                    $model->setFilename($filename);
50
51
                    return $model;
52
                }
53
            }
54
55
            throw AbstractApiException::fromApiResponse($api_response);
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * @throws MethodNotImplementedException
63
     */
64
    public function get(mixed $id): ?AbstractModel
65
    {
66
        throw new MethodNotImplementedException(
67
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
68
        );
69
    }
70
71
    /**
72
     * @throws MethodNotImplementedException
73
     */
74
    public function update(AbstractModel $model): ?AbstractModel
75
    {
76
        throw new MethodNotImplementedException(
77
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
78
        );
79
    }
80
81
    /**
82
     * @throws MethodNotImplementedException
83
     */
84
    public function delete(AbstractModel $model): void
85
    {
86
        throw new MethodNotImplementedException(
87
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
88
        );
89
    }
90
91
    /**
92
     * @throws MethodNotImplementedException
93
     */
94
    public function all(?string $endpoint = null): ArrayCollection
95
    {
96
        throw new MethodNotImplementedException(
97
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
98
        );
99
    }
100
}
101