RestConfigManipulator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 82
rs 10
ccs 32
cts 33
cp 0.9697

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B addResource() 0 25 5
A getDefaultConfig() 0 13 1
A getSorts() 0 9 2
A getAlias() 0 8 2
1
<?php
2
3
namespace Pgs\RestfonyBundle\Manipulator;
4
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Sensio\Bundle\GeneratorBundle\Manipulator\Manipulator;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Changes the PHP code of a YAML routing file.
11
 *
12
 * @author Lech Groblewicz <[email protected]>
13
 */
14
class RestConfigManipulator extends Manipulator
15
{
16
    private $file;
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param string $file The YAML routing file path
22
     */
23 3
    public function __construct($file)
24
    {
25 3
        $this->file = $file;
26 3
    }
27
28
    /**
29
     * Adds a routing resource at the top of the existing ones.
30
     *
31
     * @param string $bundle
32
     * @param string $entity
33
     * @param array  $fields
34
     *
35
     * @return bool true if it worked, false otherwise
36
     */
37 3
    public function addResource($bundle, $entity, $fields)
38
    {
39 3
        $code = strtolower($entity);
40
41 3
        if (!file_exists($this->file)) {
42 1
            if (!is_writable(dirname($this->file))) {
43 1
                throw new \RuntimeException('Rest config file doesn\'t exist');
44
            }
45
            touch($this->file);
46
        }
47
48 2
        $yaml = Yaml::parse(file_get_contents($this->file), Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
49
50 2
        if (!isset($yaml['pgs_restfony']['modules'])) {
51 1
            $yaml['pgs_restfony']['modules'] = [];
52
        }
53
54 2
        $yaml['pgs_restfony']['modules'][$code] = $this->getDefaultConfig($bundle, $entity, $fields);
55
56 2
        if (false === file_put_contents($this->file, Yaml::dump($yaml, 5))) {
57 1
            return false;
58
        }
59
60 1
        return true;
61
    }
62
63 2
    protected function getDefaultConfig($bundle, $entity, $fields)
64
    {
65 2
        $alias = $this->getAlias($entity);
66
67
        return [
68 2
            'entity' => sprintf('%s\Entity\%s', $bundle, $entity),
69 2
            'form' => sprintf('%s\Form\Type\%sType', $bundle, $entity),
70 2
            'filter' => sprintf('%s\Form\Filter\%sFilterType', $bundle, $entity),
71 2
            'controller' => sprintf('%s\Controller\%sController', $bundle, $entity),
72 2
            'manager' => sprintf('%s\Manager\%sManager', $bundle, $entity),
73 2
            'sorts' => $this->getSorts($fields[0], $alias),
74
        ];
75
    }
76
77 2
    protected function getSorts(ClassMetadata $fields, $alias)
78
    {
79 2
        $sorts = [];
80 2
        foreach ($fields->fieldNames as $field) {
81 1
            $sorts[$field] = $alias.'.'.$field;
82
        }
83
84 2
        return $sorts;
85
    }
86
87 2
    protected function getAlias($name)
88
    {
89 2
        if (preg_match_all('#([A-Z]+)#', $name, $matches)) {
90 1
            return strtolower(implode('', $matches[1]));
91
        } else {
92 1
            return '';
93
        }
94
    }
95
}
96