CoreData::createOrganizationElements()   C
last analyzed

Complexity

Conditions 8
Paths 54

Size

Total Lines 115
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
dl 0
loc 115
rs 5.2676
c 9
b 0
f 0
cc 8
eloc 80
nc 54
nop 1

How to fix   Long Method   

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
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Service;
22
23
use AppBundle\Entity\Actor;
24
use AppBundle\Entity\Documentation\Folder;
25
use AppBundle\Entity\Element;
26
use AppBundle\Entity\Organization;
27
use AppBundle\Entity\Profile;
28
use AppBundle\Entity\Reference;
29
use Doctrine\Common\Persistence\ObjectManager;
30
use Symfony\Component\Translation\TranslatorInterface;
31
32
class CoreData
33
{
34
    private $translator;
35
    private $entityManager;
36
37
    public function __construct(TranslatorInterface $translator, ObjectManager $entityManager)
38
    {
39
        $this->translator = $translator;
40
        $this->entityManager = $entityManager;
41
    }
42
43
    /**
44
     * Crea los elementos básicos de una organización
45
     *
46
     * @param Organization $organization
47
     *
48
     * @return Element
49
     */
50
    public function createOrganizationElements(Organization $organization)
51
    {
52
        $profilesData = [
53
            'teacher' => [],
54
            'tutor' => [],
55
            'department_head' => [],
56
            'financial_manager' => [],
57
            'student' => [],
58
            'head_teacher' => [],
59
            'staff' => []
60
        ];
61
62
        $profiles = [];
63
64
        $em = $this->entityManager;
65
        foreach ($profilesData as $key => $profileData) {
66
            $profile = new Profile();
67
            $profile
68
                ->setOrganization($organization)
69
                ->setCode($key)
70
                ->setNameNeutral($this->translator->trans('profile.'.$key.'.0', [], 'core'))
71
                ->setNameMale($this->translator->trans('profile.'.$key.'.1', [], 'core'))
72
                ->setNameFemale($this->translator->trans('profile.'.$key.'.2', [], 'core'))
73
                ->setInitials($this->translator->trans('profile.'.$key.'.initials', [], 'core'));
74
75
            $profiles[$key] = $profile;
76
77
            $em->persist($profile);
78
        }
79
80
        $data = [
81
            'management' => [false, [], []],
82
            'department' => ['department_head', [], []],
83
            'unit' => ['tutor', [], [
84
                'tutor',
85
                'student'
86
            ]],
87
            'evaluation' => [false, [], []],
88
            'subject' => ['teacher', [
89
                'department' => [false, false],
90
                'unit' => [true, false],
91
                'evaluation' => [true, true]
92
            ], [
93
                'teacher',
94
                'student']
95
            ],
96
            'other' => [false, [], []]
97
        ];
98
99
        $elements = [];
100
101
        $documentRoot = new Folder();
102
        $documentRoot
103
            ->setOrganization($organization)
104
            ->setName('documents');
105
106
        $em->persist($documentRoot);
107
108
        $root = new Element();
109
        $root
110
            ->setOrganization($organization)
111
            ->setFolder(true)
112
            ->setName($organization->getName());
113
114
        $em->persist($root);
115
116
        foreach ($data as $key => $item) {
117
            $element = new Element();
118
            $element
119
                ->setOrganization($organization)
120
                ->setParent($root)
121
                ->setCode($key)
122
                ->setFolder(true)
123
                ->setLocked(true)
124
                ->setName($this->translator->trans('list.'.$key, [], 'core'));
125
126
            $em->persist($element);
127
128
            if (false !== $item[0]) {
129
                $element->setProfile($profiles[$item[0]]);
130
            }
131
132
            $elements[$key] = $element;
133
        }
134
135
        // referencias
136
        foreach ($data as $key => $item) {
137
            foreach ($item[1] as $name => $referenceData) {
138
                $reference = new Reference();
139
                $reference
140
                    ->setSource($elements[$key])
141
                    ->setTarget($elements[$name])
142
                    ->setMandatory($referenceData[0])
143
                    ->setMultiple($referenceData[1]);
144
145
                $elements[$key]->addReference($reference);
146
                $em->persist($reference);
147
            }
148
        }
149
150
        // actores
151
        foreach ($data as $key => $item) {
152
            foreach ($item[2] as $actorData) {
153
                $actor = new Actor();
154
                $actor
155
                    ->setSource($elements[$key])
156
                    ->setProfile($profiles[$actorData]);
157
                $elements[$key]->addActor($actor);
158
159
                $em->persist($actor);
160
            }
161
        }
162
163
        return $root;
164
    }
165
}
166