setUrlWithReflection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 3
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\JSendSerializer;
12
13
use Illuminate\Support\Facades\Cache;
14
use Illuminate\Support\ServiceProvider;
15
use NilPortugues\Api\JSend\JSendTransformer;
16
use NilPortugues\Api\Mapping\Mapping;
17
use NilPortugues\Laravel5\JSendSerializer\Mapper\Mapper;
18
use ReflectionClass;
19
20
class Laravel5JSendSerializerServiceProvider extends ServiceProvider
21
{
22
    const PATH = '/../../../config/jsend.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('jsend.php')]);
37
    }
38
39
    /**
40
     * Register the service provider.
41
     */
42
    public function register()
43
    {
44
        $this->mergeConfigFrom(__DIR__.self::PATH, 'jsend');
45
        $this->app->singleton(\NilPortugues\Laravel5\JSendSerializer\JSendSerializer::class, function ($app) {
46
                $mapping = $app['config']->get('jsend');
47
                $key = md5(json_encode($mapping));
48
49
                return Cache::rememberForever($key, function () use ($mapping) {
50
                    return new JSendSerializer(new JSendTransformer(self::parseRoutes(new Mapper($mapping))));
51
                });
52
            });
53
    }
54
55
56
    /**
57
     * @param Mapper $mapper
58
     *
59
     * @return Mapper
60
     */
61
    private static function parseRoutes(Mapper $mapper)
62
    {
63
        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...
64
65
            $mappingClass = new \ReflectionClass($mapping);
66
67
            self::setUrlWithReflection($mapping, $mappingClass, 'resourceUrlPattern');
68
            self::setUrlWithReflection($mapping, $mappingClass, 'selfUrl');
69
            $mappingProperty = $mappingClass->getProperty('otherUrls');
70
            $mappingProperty->setAccessible(true);
71
72
            $otherUrls = (array) $mappingProperty->getValue($mapping);
73
            if(!empty($otherUrls)) {
74
                foreach ($otherUrls as &$url) {
75
                    $url = urldecode(route($url));
76
                }
77
            }
78
            $mappingProperty->setValue($mapping, $otherUrls);
79
80
        }
81
82
        return $mapper;
83
    }
84
85
86
    /**
87
     * @param Mapping         $mapping
88
     * @param ReflectionClass $mappingClass
89
     * @param string          $property
90
     */
91
    private static function setUrlWithReflection(Mapping $mapping, ReflectionClass $mappingClass, $property)
92
    {
93
        $mappingProperty = $mappingClass->getProperty($property);
94
        $mappingProperty->setAccessible(true);
95
        $value = $mappingProperty->getValue($mapping);
96
97
        if(!empty($value)) {
98
            $value = urldecode(route($value));
99
            $mappingProperty->setValue($mapping, $value);
100
        }
101
    }
102
103
    /**
104
     * Get the services provided by the provider.
105
     *
106
     * @return array
107
     */
108
    public function provides()
109
    {
110
        return ['jsend'];
111
    }
112
}
113