BasePresenter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 18.8 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 25
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
getLanguageId() 0 1 ?
A startUp() 0 4 1
A getSettings() 0 12 2
A injectEntityManager() 0 10 2
A generateSitemap() 17 17 4
A getSitemapLink() 8 8 2
A getAllLanguages() 0 11 2
A createObject() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Webcms admin module package.
5
 */
6
namespace WebCMS2\Common;
7
8
/**
9
 *
10
 */
11
abstract class BasePresenter extends \Nette\Application\UI\Presenter
12
{
13
    /**
14
     *
15
     * @var [type]
16
     */
17
    protected $em;
18
19
    /**
20
     *
21
     *
22
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
23
     */
24
    abstract protected function getLanguageId();
25
26
    /**
27
     *
28
     *
29
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
30
     */
31
    protected function startUp()
32
    {
33
        parent::startUp();
34
    }
35
36
    /**
37
     *
38
     *
39
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
40
     */
41
    protected function getSettings()
42
    {
43
        $query = $this->em->createQuery('SELECT s FROM WebCMS\Entity\Setting s WHERE s.language = '.$this->getLanguageId().' OR s.language IS NULL');
44
        $tmp = $query->getResult();
45
46
        $settings = array();
47
        foreach ($tmp as $s) {
48
            $settings[$s->getSection()][$s->getKey()] = $s;
49
        }
50
51
        return $settings;
52
    }
53
54
    /**
55
     * Injects entity manager.
56
     *
57
     * @param \Doctrine\ORM\EntityManager $em
58
     *
59
     * @return BasePresenter
60
     * @throws \Nette\InvalidStateException
61
     */
62
    public function injectEntityManager(\Doctrine\ORM\EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
63
    {
64
        if ($this->em) {
65
            throw new \Nette\InvalidStateException('Entity manager has been already set.');
66
        }
67
68
        $this->em = $em;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Generate sitemap.xml file in www (public) directory.
75
     *
76
     * @return XML sitemap
77
     */
78 View Code Duplication
    public function generateSitemap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $sitemapXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
81
82
        $repository = $this->em->getRepository('WebCMS\Entity\Page');
83
        $pages = $repository->findAll();
84
85
        foreach ($pages as $page) {
86
            if ($page->getParent() !== null && $page->getVisible()) {
87
                $sitemapXml .= "<url>\n\t<loc>".$this->getSitemapLink($page)."</loc>\n</url>\n";
88
            }
89
        }
90
91
        $sitemapXml .= '</urlset>';
92
93
        file_put_contents('./sitemap.xml', $sitemapXml);
94
    }
95
96
    /**
97
     * Get single sitemap link url address.
98
     *
99
     * @param  \WebCMS2\Entity\Page $page Page entity object.
100
     * @return string               Url address of the link.
101
     */
102 View Code Duplication
    private function getSitemapLink($page)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $url = $this->context->httpRequest->url->baseUrl;
0 ignored issues
show
Documentation introduced by
The property $context is declared private in Nette\Application\UI\Presenter. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
105
        $url .= !$page->getLanguage()->getDefaultFrontend() ? $page->getLanguage()->getAbbr().'/' : '';
106
        $url .= $page->getPath();
107
108
        return $url;
109
    }
110
111
    /**
112
     *
113
     *
114
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
115
     */
116
    protected function getAllLanguages()
117
    {
118
        $languages = $this->em->getRepository('WebCMS\Entity\Language')->findAll();
119
120
        $langs = array('' => $this->translation['Pick a language']);
0 ignored issues
show
Documentation introduced by
The property translation does not exist on object<WebCMS2\Common\BasePresenter>. 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...
121
        foreach ($languages as $l) {
122
            $langs[$l->getId()] = $l->getName();
123
        }
124
125
        return $langs;
126
    }
127
128
    /**
129
     *
130
     *
131
     * @param  [type] $name [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
132
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
133
     */
134
    protected function createObject($name)
135
    {
136
        $expl = explode('-', $name);
137
138
        $objectName = ucfirst($expl[0]);
139
        $objectName = "\\WebCMS\\$objectName"."Module\\".$objectName;
140
141
        return new $objectName();
142
    }
143
}
144