Completed
Push — wip-subtree ( 04b705...405079 )
by
unknown
12:15
created

ServicesManipulator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C addResource() 0 52 8
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\Generator;
14
15
use Sonata\AdminBundle\Manipulator\ServicesManipulator as BaseManipulator;
16
use Symfony\Component\Yaml\Yaml;
17
18
/**
19
 * Class ServicesManipulator.
20
 */
21
class ServicesManipulator extends BaseManipulator
22
{
23
    /**
24
     * @var string
25
     */
26
    private $template = '    %s:
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
        class: %s
28
        arguments: [~, %s, %s]
29
        tags:
30
            - name: sonata.admin
31
              manager_type: orm
32
              group: admin
33
              label: %s
34
35
';
36
37
    /**
38
     * @param string $file
39
     */
40
    public function __construct($file)
41
    {
42
        $this->file = (string) $file;
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
43
    }
44
45
    /**
46
     * @param string $serviceId
47
     * @param string $modelClass
48
     * @param string $adminClass
49
     * @param string $controllerName
50
     * @param string $managerType
51
     *
52
     * @throws \RuntimeException
53
     */
54
    public function addResource($serviceId, $modelClass, $adminClass, $controllerName, $managerType)
55
    {
56
        $rc = new \ReflectionClass($modelClass);
57
        $modelClassShortName = $rc->getShortName();
58
59
        $code = "services:\n";
60
61
        if (is_file($this->file)) {
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
62
            $code = rtrim(file_get_contents($this->file));
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
63
            $data = (array) Yaml::parse($code);
64
65
            if ($code !== '') {
66
                $code .= "\n";
67
            }
68
69
            if (array_key_exists('services', $data)) {
70
                if (array_key_exists($serviceId, (array) $data['services'])) {
71
                    throw new \RuntimeException(sprintf(
72
                        'The service "%s" is already defined in the file "%s".',
73
                        $serviceId,
74
                        realpath($this->file)
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
75
                    ));
76
                }
77
78
                if ($data['services'] !== null) {
79
                    $code .= "\n";
80
                }
81
            } else {
82
                $code .= $code === '' ? '' : "\n" . "services:\n";
83
            }
84
        }
85
86
        $code .= sprintf(
87
            $this->template,
88
            $serviceId,
89
            $adminClass,
90
            $modelClass,
91
            $controllerName,
92
            $modelClassShortName,
93
            $managerType,
94
            current(array_slice(explode('\\', $modelClass), -1))
95
        );
96
        @mkdir(dirname($this->file), 0777, true);
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
98
        if (@file_put_contents($this->file, $code) === false) {
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
99
            throw new \RuntimeException(sprintf(
100
                'Unable to append service "%s" to the file "%s". You will have to do it manually.',
101
                $serviceId,
102
                $this->file
0 ignored issues
show
Bug introduced by
The property file cannot be accessed from this context as it is declared private in class Sonata\AdminBundle\Manipulator\ServicesManipulator.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
103
            ));
104
        }
105
    }
106
}
107