Test Failed
Pull Request — master (#23)
by
unknown
10:17
created

TransformerResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 22
dl 0
loc 63
ccs 0
cts 22
cp 0
rs 10
c 4
b 1
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 30 5
1
<?php
2
3
namespace Rexlabs\Laravel\Smokescreen\Transformers;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException;
8
use Rexlabs\Smokescreen\Resource\ResourceInterface;
0 ignored issues
show
Bug introduced by
The type Rexlabs\Smokescreen\Resource\ResourceInterface 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 Rexlabs\Smokescreen\Transformer\TransformerResolverInterface;
0 ignored issues
show
Bug introduced by
The type Rexlabs\Smokescreen\Tran...formerResolverInterface 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...
10
11
class TransformerResolver implements TransformerResolverInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $namespace;
17
18
    /**
19
     * @var string
20
     */
21
    protected $nameTemplate;
22
23
    /**
24
     * TransformerResolver constructor.
25
     *
26
     * @param string $namespace
27
     * @param string $nameTemplate
28
     */
29
    public function __construct(string $namespace, string $nameTemplate)
30
    {
31
        $this->namespace = $namespace;
32
        $this->nameTemplate = $nameTemplate;
33
    }
34
35
    /**
36
     * Determines the Transformer object to be used for a particular resource.
37
     * Inspects the underlying Eloquent model to determine an appropriately
38
     * named transformer class, and instantiate the object.
39
     *
40
     * {@inheritdoc}
41
     *
42
     * @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException
43
     */
44
    public function resolve(ResourceInterface $resource)
45
    {
46
        $transformer = null;
47
48
        // Find the underlying model of the resource data
49
        $model = null;
50
        $data = $resource->getData();
51
        if ($data instanceof Model) {
52
            $model = $data;
53
        } elseif ($data instanceof Collection) {
54
            $model = $data->first();
55
        }
56
57
        if ($model !== null) {
58
            // Cool, now let's try to find a matching transformer based on our Model class
59
            // We use our configuration value 'transformer_namespace' to determine where to look.
60
            try {
61
                $modelName = (new \ReflectionClass($model))->getShortName();
62
                $transformerName = preg_replace('/{ModelName}/i', $modelName, $this->nameTemplate);
63
                $transformer = resolve(sprintf('%s\\%s', $this->namespace, $transformerName));
64
            } catch (\Exception $e) {
65
                throw new UnresolvedTransformerException(
66
                    'Unable to resolve transformer for model: ' . \get_class($model),
67
                    0,
68
                    $e
69
                );
70
            }
71
        }
72
73
        return $transformer;
74
    }
75
}
76