Completed
Push — develop ( 085115...b1372b )
by greg
30:06
created

Country::getAllCountries()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 7
Ratio 41.18 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 7
loc 17
rs 9.2
cc 4
eloc 9
nc 6
nop 1
1
<?php
2
namespace PlaygroundCore\Service;
3
4
use Zend\ServiceManager\ServiceManagerAwareInterface;
5
use Zend\ServiceManager\ServiceManager;
6
use ZfcBase\EventManager\EventProvider;
7
8
final class Country extends EventProvider implements ServiceManagerAwareInterface
9
{
10
    private $translatedTo;
11
12
    private $path;
13
14
    private $corePath;
15
16
    public function getTranslatedTo()
17
    {
18
        return $this->translatedTo;
19
    }
20
21
    public function setTranslatedTo($translatedTo)
22
    {
23
        $this->translatedTo = (string) $translatedTo;
24
25
        return $this;
26
    }
27
28
    public function getPath()
29
    {
30
        if(empty($this->path)){
31
            $this->path = str_replace('\\', '/', getcwd()) . '/language/countries';
32
        }
33
34
        return $this->path;
35
    }
36
37
    public function getCorePath()
38
    {
39
        if(empty($this->corePath)){
40
            $this->corePath = __DIR__ . '/../../../language/countries';
41
        }
42
43
        return $this->corePath;
44
    }
45
46
    public function getAllCountries($translatedTo = null)
47
    {
48
        if (null === $translatedTo) {
49
            $translatedTo = $this->getServiceManager()->get('translator')->getLocale();
50
        }
51
        
52
        $fileName = $this->getPath().'/'.$translatedTo.'.php';
53 View Code Duplication
        if (! file_exists($fileName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            $fileName = $this->getCorePath().'/'.$translatedTo.'.php';
55
56
            if (! file_exists($fileName)) {
57
                throw new \InvalidArgumentException("Language $translatedTo not found.");
58
            }
59
        }
60
61
        return include $fileName;
62
    }
63
64
    public function getCountry($country, $translatedTo = null)
65
    {
66
        if (null === $translatedTo) {
67
            $translatedTo = $this->getServiceManager()->get('translator')->getLocale();
68
        }
69
        $fileName = $this->getPath().'/'.$translatedTo.'.php';
70 View Code Duplication
        if (! file_exists($fileName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71
            $fileName = $this->getCorePath().'/'.$translatedTo.'.php';
72
            if (! file_exists($fileName)) {
73
                throw new \InvalidArgumentException("Language $translatedTo not found.");
74
            }
75
        }
76
77
        $list = include $fileName;
78
        if (!is_array($list)) {
79
            throw new \InvalidArgumentException("Language $translatedTo not found.");
80
        }
81
        $country = strtoupper($country);
82
        if (!array_key_exists($country, $list)) {
83
            throw new \InvalidArgumentException("Country $country not found for $translatedTo.");
84
        }
85
86
        return $list[$country];
87
    }
88
89
    /**
90
     * Retrieve service manager instance
91
     *
92
     * @return ServiceManager
93
     */
94
    public function getServiceManager()
95
    {
96
        return $this->serviceManager;
0 ignored issues
show
Bug introduced by
The property serviceManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
97
    }
98
99
    /**
100
     * Set service manager instance
101
     *
102
     * @param  ServiceManager $serviceManager
103
     * @return User
104
     */
105
    public function setServiceManager(ServiceManager $serviceManager)
106
    {
107
        $this->serviceManager = $serviceManager;
108
109
        return $this;
110
    }
111
}
112