Completed
Pull Request — master (#10)
by Jodie
04:24
created

TransformerResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B resolve() 0 29 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;
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
    /** @var string|null */
14
    protected $namespace;
15
16
    /**
17
     * TransformerResolver constructor.
18
     *
19
     * @param string $namespace
20
     */
21
    public function __construct(string $namespace)
22
    {
23
        $this->namespace = $namespace;
24
    }
25
26
    /**
27
     * Determines the Transformer object to be used for a particular resource.
28
     * Inspects the underlying Eloquent model to determine an appropriately
29
     * named transformer class, and instantiate the object.
30
     *
31
     * @inheritdoc
32
     * @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException
33
     */
34
    public function resolve(ResourceInterface $resource)
35
    {
36
        $transformer = null;
37
38
        // Find the underlying model of the resource data
39
        $model = null;
40
        $data = $resource->getData();
41
        if ($data instanceof Model) {
42
            $model = $data;
43
        } elseif ($data instanceof Collection) {
44
            $model = $data->first();
45
        }
46
47
        // If no model can be determined from the data
48
        if ($model !== null) {
49
            // Cool, now let's try to find a matching transformer based on our Model class
50
            // We use our configuration value 'transformer_namespace' to determine where to look.
51
            try {
52
                $transformerClass = sprintf('%s\\%sTransformer',
53
                    $this->namespace,
54
                    (new \ReflectionClass($model))->getShortName());
55
                $transformer = resolve($transformerClass);
56
            } catch (\Exception $e) {
57
                throw new UnresolvedTransformerException('Unable to resolve transformer for model: ' . \get_class($model),
58
                    0, $e);
59
            }
60
        }
61
62
        return $transformer;
63
    }
64
}