Passed
Push — master ( 11cefe...18a28d )
by Darko
11:42
created

ApiTransformer::nullIfZero()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace App\Transformers;
4
5
use App\Models\Category;
6
use App\Models\Release;
7
use App\Models\User;
8
use Illuminate\Support\Carbon;
9
use League\Fractal\TransformerAbstract;
10
11
class ApiTransformer extends TransformerAbstract
12
{
13
    protected User $user;
14
15
    /**
16
     * ApiTransformer constructor.
17
     *
18
     * @param  User  $user  The authenticated user for API access
19
     */
20
    public function __construct(User $user)
21
    {
22
        $this->user = $user;
23
    }
24
25
    /**
26
     * Transform a release into an API response array.
27
     *
28
     * @param  Release  $release  The release to transform
29
     * @return array The transformed release data
30
     */
31
    public function transform(Release $release): array
32
    {
33
        $data = $this->getBaseData($release);
34
35
        if (\in_array($release->categories_id, Category::MOVIES_GROUP, false)) {
0 ignored issues
show
Bug introduced by
The property categories_id does not exist on App\Models\Release. Did you mean category_ids?
Loading history...
36
            return array_merge($data, $this->getMovieSpecificData($release));
37
        }
38
39
        if (\in_array($release->categories_id, Category::TV_GROUP, false)) {
40
            return array_merge($data, $this->getTvSpecificData($release));
41
        }
42
43
        return $data;
44
    }
45
46
    /**
47
     * Get base data common to all releases.
48
     */
49
    protected function getBaseData(Release $release): array
50
    {
51
        return [
52
            'title' => $release->searchname,
0 ignored issues
show
Bug introduced by
The property searchname does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
53
            'details' => $this->getDetailsUrl($release->guid),
0 ignored issues
show
Bug introduced by
The property guid does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
54
            'url' => $this->getDownloadUrl($release->guid),
55
            'category' => $release->categories_id,
0 ignored issues
show
Bug introduced by
The property categories_id does not exist on App\Models\Release. Did you mean category_ids?
Loading history...
56
            'category_name' => $release->category_name,
57
            'added' => Carbon::parse($release->adddate)->toRssString(),
0 ignored issues
show
Bug introduced by
The property adddate does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
58
            'size' => $release->size,
0 ignored issues
show
Bug introduced by
The property size does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
59
            'files' => $release->totalpart,
0 ignored issues
show
Bug introduced by
The property totalpart does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
60
            'grabs' => $this->nullIfZero($release->grabs),
0 ignored issues
show
Bug introduced by
The property grabs does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
61
            'comments' => $this->nullIfZero($release->comments),
0 ignored issues
show
Bug introduced by
The property comments does not exist on App\Models\Release. Did you mean comment?
Loading history...
62
            'password' => $release->passwordstatus,
0 ignored issues
show
Bug introduced by
The property passwordstatus does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
63
            'usenetdate' => Carbon::parse($release->postdate)->toRssString(),
0 ignored issues
show
Bug introduced by
The property postdate does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
64
        ];
65
    }
66
67
    /**
68
     * Get movie-specific data fields.
69
     */
70
    protected function getMovieSpecificData(Release $release): array
71
    {
72
        return [
73
            'imdbid' => $this->nullIfZero($release->imdbid),
0 ignored issues
show
Bug introduced by
The property imdbid does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
74
            'tmdbid' => $this->nullIfZero($release->tmdbid),
0 ignored issues
show
Bug introduced by
The property tmdbid does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
75
            'traktid' => $this->nullIfZero($release->traktid),
0 ignored issues
show
Bug introduced by
The property traktid does not exist on App\Models\Release. Did you mean trakt?
Loading history...
76
        ];
77
    }
78
79
    /**
80
     * Get TV-specific data fields.
81
     */
82
    protected function getTvSpecificData(Release $release): array
83
    {
84
        return [
85
            'episode_title' => $release->title ?? $this->null(),
86
            'season' => $release->series ?? $this->null(),
0 ignored issues
show
Bug introduced by
The property series does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
87
            'episode' => $release->episode ?? $this->null(),
88
            'tvairdate' => $release->firstaired ?? $this->null(),
89
            'tvdbid' => $this->nullIfZero($release->tvdb),
90
            'traktid' => $this->nullIfZero($release->trakt),
91
            'tvrageid' => $this->nullIfZero($release->tvrage),
92
            'tvmazeid' => $this->nullIfZero($release->tvmaze),
93
            'imdbid' => $this->nullIfZero($release->imdb),
0 ignored issues
show
Bug introduced by
The property imdb does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
94
            'tmdbid' => $this->nullIfZero($release->tmdb),
0 ignored issues
show
Bug introduced by
The property tmdb does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
95
        ];
96
    }
97
98
    /**
99
     * Generate the details URL for a release.
100
     */
101
    protected function getDetailsUrl(string $guid): string
102
    {
103
        return url('/details/'.$guid);
104
    }
105
106
    /**
107
     * Generate the download URL for a release.
108
     */
109
    protected function getDownloadUrl(string $guid): string
110
    {
111
        return url('/getnzb').'?id='.$guid.'.nzb&r='.$this->user->api_token;
112
    }
113
114
    /**
115
     * Return null if the value is zero, otherwise return the value.
116
     *
117
     * @param  mixed  $value
118
     * @return mixed
119
     */
120
    protected function nullIfZero($value)
121
    {
122
        return ($value !== null && $value !== 0) ? $value : $this->null();
123
    }
124
}
125