PostNavigateResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 11
c 3
b 2
f 0
dl 0
loc 27
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 20 6
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Resources\Post;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Resources\Json\JsonResource;
7
8
/**
9
 * Post Navigate Resource for combined previous/next response
10
 */
11
class PostNavigateResource extends JsonResource
12
{
13
    /**
14
     * @param  Request  $request
15
     *
16
     * @return array<string, mixed>
17
     */
18
    public function toArray($request): array
19
    {
20
        // Ensure the resource is an array with the expected structure
21
        if (!is_array($this->resource)) {
22
            return [
23
                'previous' => null,
24
                'next' => null,
25
            ];
26
        }
27
28
        // Cast to proper array type for PHPStan
29
        /** @var array<string, null|\CSlant\Blog\Core\Models\Post> $navigationData */
30
        $navigationData = $this->resource;
31
32
        $previous = array_key_exists('previous', $navigationData) ? $navigationData['previous'] : null;
33
        $next = array_key_exists('next', $navigationData) ? $navigationData['next'] : null;
34
35
        return [
36
            'previous' => $previous ? new PostNavigationResource($previous) : null,
37
            'next' => $next ? new PostNavigationResource($next) : null,
38
        ];
39
    }
40
}
41