RoutingManipulator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setYamlPrefix() 0 4 1
B addResource() 0 38 10
1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Routing\Manipulator;
4
5
/**
6
 * Changes the PHP code of a YAML routing file.
7
 *
8
 * @author Fabien Potencier <[email protected]>
9
 */
10
use Sensio\Bundle\GeneratorBundle\Manipulator\Manipulator;
11
use Symfony\Component\DependencyInjection\Container;
12
13
class RoutingManipulator extends Manipulator
14
{
15
    private $file;
16
17
    protected $yaml_prefix;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param string $file The YAML routing file path
23
     */
24
    public function __construct($file)
25
    {
26
        $this->file = $file;
27
    }
28
29
    public function setYamlPrefix($yaml_prefix)
30
    {
31
        $this->yaml_prefix = $yaml_prefix;
32
    }
33
34
    /**
35
     * Adds a routing resource at the top of the existing ones.
36
     *
37
     * @param string $bundle
38
     * @param string $format
39
     * @param string $prefix
40
     * @param string $path
41
     *
42
     * @return Boolean true if it worked, false otherwise
43
     *
44
     * @throws \RuntimeException If bundle is already imported
45
     */
46
    public function addResource($bundle, $format, $prefix = null, $path = 'routing')
47
    {
48
        $current = '';
49
50
        if (null === $prefix) {
51
            $prefix = '/admin/'.Container::underscore($bundle). ($this->yaml_prefix ? '/'.$this->yaml_prefix : '');
52
        }
53
54
        $routing_name=$bundle.('/' !== $prefix ? '_'.str_replace('/', '_', substr($prefix, 1)) : '');
55
        if (file_exists($this->file)) {
56
            $current = file_get_contents($this->file);
57
58
            // Don't add same bundle twice
59
            if (false !== strpos($current, $routing_name)) {
60
                throw new \RuntimeException(sprintf('Bundle "%s" is already imported.', $bundle));
61
            }
62
        } elseif (!is_dir($dir = dirname(realpath($this->file)))) {
63
            mkdir($dir, 0777, true);
64
        }
65
66
67
68
        $code = sprintf("%s:\n", $routing_name);
69
        if ('admingenerator' == $format) {
70
            $code .= sprintf("    resource: \"@%s/Controller/%s\"\n    type:     admingenerator\n", $bundle, $this->yaml_prefix ? ucfirst($this->yaml_prefix).'/' : '');
71
        } else {
72
            $code .= sprintf("    resource: \"@%s/Resources/config/%s.%s\"\n", $bundle, $path, $format);
73
        }
74
        $code .= sprintf("    prefix:   %s\n", $prefix);
75
        $code .= "\n";
76
        $code .= $current;
77
78
        if (false === file_put_contents($this->file, $code)) {
79
            return false;
80
        }
81
82
        return true;
83
    }
84
}
85