Transformer::callIncludeMethod()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 3
1
<?php
2
3
namespace Apiato\Core\Abstracts\Transformers;
4
5
use Apiato\Core\Exceptions\CoreInternalErrorException;
6
use Apiato\Core\Exceptions\UnsupportedFractalIncludeException;
7
use Apiato\Core\Foundation\Facades\Apiato;
8
use ErrorException;
9
use Exception;
10
use Illuminate\Support\Facades\Config;
11
use League\Fractal\Scope;
12
use League\Fractal\TransformerAbstract as FractalTransformer;
13
14
/**
15
 * Class Transformer.
16
 *
17
 * @author  Mahmoud Zalt <[email protected]>
18
 */
19
abstract class Transformer extends FractalTransformer
20
{
21
22
    /**
23
     * @return  mixed
24
     */
25
    public function user()
26
    {
27
        return Apiato::call('[email protected]ticatedUserTask');
28
    }
29
30
    /**
31
     * @param $adminResponse
32
     * @param $clientResponse
33
     *
34
     * @return  array
35
     */
36
    public function ifAdmin($adminResponse, $clientResponse)
37
    {
38
        $user = $this->user();
39
40
        if (!is_null($user) && $user->hasAdminRole()) {
41
            return array_merge($clientResponse, $adminResponse);
42
        }
43
44
        return $clientResponse;
45
    }
46
47
    /**
48
     * @param mixed                       $data
49
     * @param callable|FractalTransformer $transformer
50
     * @param null                        $resourceKey
51
     *
52
     * @return \League\Fractal\Resource\Item
53
     */
54
    public function item($data, $transformer, $resourceKey = null)
55
    {
56
        // set a default resource key if none is set
57
        if (!$resourceKey && $data) {
58
            $resourceKey = $data->getResourceKey();
59
        }
60
61
        return parent::item($data, $transformer, $resourceKey);
62
    }
63
64
    /**
65
     * @param mixed                       $data
66
     * @param callable|FractalTransformer $transformer
67
     * @param null                        $resourceKey
68
     *
69
     * @return \League\Fractal\Resource\Collection
70
     */
71
    public function collection($data, $transformer, $resourceKey = null)
72
    {
73
        // set a default resource key if none is set
74
        if (!$resourceKey && $data->isNotEmpty()) {
75
            $resourceKey = (string) $data->modelKeys()[0];
76
        }
77
78
        return parent::collection($data, $transformer, $resourceKey);
79
    }
80
81
    /**
82
     * @param Scope  $scope
83
     * @param string $includeName
84
     * @param mixed  $data
85
     *
86
     * @return \League\Fractal\Resource\ResourceInterface
87
     * @throws CoreInternalErrorException
88
     * @throws UnsupportedFractalIncludeException
89
     */
90
    protected function callIncludeMethod(Scope $scope, $includeName, $data)
91
    {
92
        try {
93
            return parent::callIncludeMethod($scope, $includeName, $data);
94
        }
95
        catch (ErrorException $exception) {
0 ignored issues
show
Bug introduced by Johannes Schobel
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
96
            if (Config::get('apiato.requests.force-valid-includes', true)) {
97
                throw new UnsupportedFractalIncludeException($exception->getMessage());
98
            }
99
        }
100
        catch (Exception $exception) {
101
            throw new CoreInternalErrorException($exception->getMessage());
102
        }
103
    }
104
105
}
106