Laravel5JsonSerializerServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 12 1
B parseRoutes() 0 22 4
A setUrlWithReflection() 0 11 2
A provides() 0 4 1
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 8/15/15
6
 * Time: 5:45 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Laravel5\JsonSerializer;
12
13
use Illuminate\Support\Facades\Cache;
14
use Illuminate\Support\ServiceProvider;
15
use NilPortugues\Api\Json\JsonTransformer;
16
use NilPortugues\Api\Mapping\Mapping;
17
use NilPortugues\Laravel5\JsonSerializer\Mapper\Mapper;
18
use ReflectionClass;
19
20
class Laravel5JsonSerializerServiceProvider extends ServiceProvider
21
{
22
    const PATH = '/../../../config/json.php';
23
24
    /**
25
     * Indicates if loading of the provider is deferred.
26
     *
27
     * @var bool
28
     */
29
    protected $defer = false;
30
31
    /**
32
     * Bootstrap the application events.
33
     */
34
    public function boot()
35
    {
36
        $this->publishes([__DIR__.self::PATH => config('json.php')]);
37
    }
38
39
    /**
40
     * Register the service provider.
41
     */
42
    public function register()
43
    {
44
        $this->mergeConfigFrom(__DIR__.self::PATH, 'jsonapi');
45
        $this->app->singleton(\NilPortugues\Laravel5\JsonSerializer\JsonSerializer::class, function ($app) {
46
                $mapping = $app['config']->get('jsonapi');
47
                $key = md5(json_encode($mapping));
48
49
                return Cache::rememberForever($key, function () use ($mapping) {
50
                    return new JsonSerializer(new JsonTransformer(self::parseRoutes(new Mapper($mapping))));
51
                });
52
            });
53
    }
54
55
    /**
56
     * @param Mapper $mapper
57
     *
58
     * @return Mapper
59
     */
60
    private static function parseRoutes(Mapper $mapper)
61
    {
62
        foreach ($mapper->getClassMap() as &$mapping) {
0 ignored issues
show
Bug introduced by
The expression $mapper->getClassMap() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
63
64
            $mappingClass = new \ReflectionClass($mapping);
65
66
            self::setUrlWithReflection($mapping, $mappingClass, 'resourceUrlPattern');
67
            self::setUrlWithReflection($mapping, $mappingClass, 'selfUrl');
68
            $mappingProperty = $mappingClass->getProperty('otherUrls');
69
            $mappingProperty->setAccessible(true);
70
71
            $otherUrls = (array) $mappingProperty->getValue($mapping);
72
            if (!empty($otherUrls)) {
73
                foreach ($otherUrls as &$url) {
74
                    $url = urldecode(route($url));
75
                }
76
            }
77
            $mappingProperty->setValue($mapping, $otherUrls);
78
        }
79
80
        return $mapper;
81
    }
82
83
    /**
84
     * @param Mapping         $mapping
85
     * @param ReflectionClass $mappingClass
86
     * @param string          $property
87
     */
88
    private static function setUrlWithReflection(Mapping $mapping, ReflectionClass $mappingClass, $property)
89
    {
90
        $mappingProperty = $mappingClass->getProperty($property);
91
        $mappingProperty->setAccessible(true);
92
        $value = $mappingProperty->getValue($mapping);
93
94
        if (!empty($value)) {
95
            $value = urldecode(route($value));
96
            $mappingProperty->setValue($mapping, $value);
97
        }
98
    }
99
    /**
100
     * Get the services provided by the provider.
101
     *
102
     * @return array
103
     */
104
    public function provides()
105
    {
106
        return ['json'];
107
    }
108
}
109