PermessiSingletonUtility::instance()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 13
cp 0.6923
rs 9.5555
cc 5
nc 4
nop 4
crap 5.7283
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Fi\CoreBundle\Utils;
10
11
final class PermessiSingletonUtility
12
{
13
14
    private static $PermessiTabelle;
15
    private static $modulo;
16
    private static $operatore;
17
    private static $ruolo;
18
19 32
    public static function instance($em, $modulo, $operatore, $ruolo)
20
    {
21 32
        static $inst = null;
22 32
        if ($inst === null) {
23
            self::$modulo = $modulo;
24
            self::$operatore = $operatore;
25
            self::$ruolo = $ruolo;
26
            $inst = new PermessiSingletonUtility($em, $modulo, $operatore, $ruolo);
27
        }
28 32
        if ($modulo != self::$modulo || $operatore != self::$operatore || $ruolo != self::$ruolo) {
29 25
            self::$modulo = $modulo;
30 25
            self::$operatore = $operatore;
31 25
            self::$ruolo = $ruolo;
32 25
            $inst = new PermessiSingletonUtility($em, $modulo, $operatore, $ruolo);
33
        }
34 32
        return $inst;
35
    }
36
37
    /**
38
     * Private construct so nobody else can instantiate it
39
     *
40
     */
41 25
    private function __construct($em, $modulo, $operatore, $ruolo)
42
    {
43
        $q = $em
44 25
                ->getRepository('FiCoreBundle:Permessi')
45 25
                ->findOneBy(array('operatori_id' => $operatore, 'modulo' => $modulo));
46
47 25
        if (!$q) {
48
            $q = $em
49 25
                    ->getRepository('FiCoreBundle:Permessi')
50 25
                    ->findOneBy(array('ruoli_id' => $ruolo, 'modulo' => $modulo, 'operatori_id' => null));
51 25
            if (!$q) {
52
                $q = $em
53 16
                        ->getRepository('FiCoreBundle:Permessi')
54 16
                        ->findOneBy(array('ruoli_id' => null, 'modulo' => $modulo, 'operatori_id' => null));
55
            }
56
        }
57
58 25
        self::$PermessiTabelle = $q;
59 25
    }
60
61
    /**
62
     * Call this method to get singleton
63
     *
64
     * @return \Fi\CoreBundle\Entity\Permessi
65
     */
66 32
    public static function getPermessi()
67
    {
68 32
        return self::$PermessiTabelle;
69
    }
70
}
71