FractalTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 31
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 17 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Traits;
6
7
use League\Fractal\Manager;
0 ignored issues
show
Bug introduced by
The type League\Fractal\Manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use League\Fractal\Serializer\JsonApiSerializer;
0 ignored issues
show
Bug introduced by
The type League\Fractal\Serializer\JsonApiSerializer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use function Canvas\Core\envValue;
10
use function sprintf;
11
use function ucfirst;
12
13
/**
14
 * Trait FractalTrait
15
 *
16
 * @package Canvas\Traits
17
 */
18
trait FractalTrait
19
{
20
    /**
21
     * Format results based on a transformer
22
     *
23
     * @param string  $method
24
     * @param mixed   $results
25
     * @param string  $transformer
26
     * @param string  $resource
27
     * @param array   $relationships
28
     * @param array   $fields
29
     *
30
     * @return array
31
     */
32
    protected function format(string $method, $results, string $transformer, string $resource, array $relationships = [], array $fields = []): array {
33
        $url = envValue('APP_URL', 'http://localhost');
34
        $manager = new Manager();
35
        $manager->setSerializer(new JsonApiSerializer($url));
36
37
        /**
38
         * Process relationships
39
         */
40
        if (count($relationships) > 0) {
41
            $manager->parseIncludes($relationships);
42
        }
43
44
        $class = sprintf('League\Fractal\Resource\%s', ucfirst($method));
45
        $resource = new $class($results, new $transformer($fields, $resource), $resource);
46
        $results = $manager->createData($resource)->toArray();
47
48
        return $results;
49
    }
50
}
51