RouteDeclarationCompiler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 47
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileRoute() 0 5 1
A compileAllRoutes() 0 17 4
A compileToFile() 0 6 1
1
<?php
2
3
4
namespace ElementsFramework\DynamicRouting\Service\Compiler;
5
6
7
use ElementsFramework\DynamicRouting\Model\DynamicRoute;
8
use Illuminate\Support\Facades\File;
9
10
/**
11
 * Class RouteDeclarationCompiler
12
 * @package ElementsFramework\DynamicRouting\Service\Compiler
13
 */
14
class RouteDeclarationCompiler
15
{
16
17
    /**
18
     * Returns a compiled definition for the route entry.
19
     * @param DynamicRoute $dynamicRoute
20
     * @return string
21
     */
22
    public function compileRoute(DynamicRoute $dynamicRoute)
23
    {
24
        $action = 'ElementsFramework\DynamicRouting\Service\RouteClosureProvider::forRouteId('.$dynamicRoute->id.')';
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<ElementsFramework...ing\Model\DynamicRoute>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25
        return 'Route::' . $dynamicRoute->method . '("'.$dynamicRoute->pattern.'", '.$action.')->name("'.$dynamicRoute->name.'");';
0 ignored issues
show
Bug introduced by
The property method does not seem to exist. Did you mean manyMethods?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property pattern does not exist on object<ElementsFramework...ing\Model\DynamicRoute>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property name does not exist on object<ElementsFramework...ing\Model\DynamicRoute>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
    }
27
28
    /**
29
     * Returns the output for the whole dynamic routes file.
30
     * @return string
31
     */
32
    public function compileAllRoutes()
33
    {
34
        $routes = DynamicRoute::all();
35
36
        $output = "";
37
        if(!empty(config('dynamic-routing.middleware-group'))) {
38
            $output .= "Route::group(['middleware' => '".config('dynamic-routing.middleware-group')."'], function () {" . PHP_EOL;
39
        }
40
        foreach($routes as $route) {
41
            $output .= $this->compileRoute($route) . PHP_EOL;
42
        }
43
        if(!empty(config('dynamic-routing.middleware-group'))) {
44
            $output .= "});" . PHP_EOL;
45
        }
46
47
        return $output;
48
    }
49
50
    /**
51
     * Saves the compiled routes to the file set in the configuration.
52
     */
53
    public function compileToFile()
54
    {
55
        $contents = $this->compileAllRoutes();
56
        $contents = "<?php" . PHP_EOL . $contents;
57
        File::put(base_path(config('dynamic-routing.compiled-routes-path')), $contents);
58
    }
59
60
}