Completed
Push — master ( 44391b...87bc43 )
by Julito
13:49
created

BreadcrumbBlockService::getMenu()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 68
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 38
nc 48
nop 1
dl 0
loc 68
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Block;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Sonata\BlockBundle\Block\BlockContextInterface;
8
use Sonata\SeoBundle\Block\Breadcrumb\BaseBreadcrumbMenuBlockService;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * Class BreadcrumbBlockService.
13
 */
14
class BreadcrumbBlockService extends BaseBreadcrumbMenuBlockService
15
{
16
    protected $extraChildren;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getName()
22
    {
23
        return 'chamilo_core.block.breadcrumb';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function configureSettings(OptionsResolver $resolver)
30
    {
31
        parent::configureSettings($resolver);
32
33
        $resolver->setDefaults([
34
            //'menu_template' => 'SonataSeoBundle:Block:breadcrumb.html.twig',
35
            'menu_template' => '@ChamiloTheme/Breadcrumb/breadcrumb_legacy.html.twig',
36
            'include_homepage_link' => false,
37
            'context' => false,
38
        ]);
39
    }
40
41
    /**
42
     * @param string $title
43
     * @param array  $params
44
     */
45
    public function addChild($title, $params = [])
46
    {
47
        $this->extraChildren[] = ['title' => $title, 'params' => $params];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function getMenu(BlockContextInterface $blockContext)
54
    {
55
        $menu = $this->getRootMenu($blockContext);
56
57
        $menu->addChild('Home', ['route' => 'home']);
58
        $sessionId = 0;
59
        if ($blockContext->getBlock()->getSetting('session_id')) {
60
            $sessionId = $blockContext->getBlock()->getSetting('session_id');
61
        }
62
63
        // Add course
64
        /** @var Course $course */
65
        if ($course = $blockContext->getBlock()->getSetting('course')) {
66
            if (is_array($course)) {
67
                $title = $course['title'];
68
                $code = $course['code'];
69
            } else {
70
                $title = $course->getTitle();
71
                $code = $course->getCode();
72
            }
73
74
            $menu->addChild(
75
                $title,
76
                [
77
                    'route' => 'course_home',
78
                    'routeParameters' => [
79
                        'course' => $code,
80
                        'id_session' => $sessionId,
81
                    ],
82
                ]
83
            );
84
        }
85
86
        if (!empty($this->extraChildren)) {
87
            foreach ($this->extraChildren as $item) {
88
                $params = isset($item['params']) ? $item['params'] : [];
89
                $menu->addChild(
90
                    $item['title'],
91
                    $params
92
                );
93
            }
94
        }
95
96
        // Load legacy breadcrumbs
97
        $oldBreadCrumb = $blockContext->getBlock()->getSetting('legacy_breadcrumb');
98
99
        if ($oldBreadCrumb) {
100
            foreach ($oldBreadCrumb as $data) {
101
                if (empty($data['name'])) {
102
                    continue;
103
                }
104
                $url = $data['url'];
105
                if ($url === '#') {
106
                    $menu->addChild($data['name']);
107
                } else {
108
                    $menu->addChild($data['name'], ['uri' => $url]);
109
                }
110
            }
111
        }
112
113
        // Set CSS classes for the items
114
        foreach ($menu->getChildren() as $child) {
115
            $child
116
                //->setLinkAttribute('class', 'nav-link')
117
                ->setAttribute('class', 'breadcrumb-item');
118
        }
119
120
        return $menu;
121
    }
122
}
123