Completed
Push — develop ( afe0ec...7c3dfe )
by
unknown
21:46 queued 13:34
created

IndexController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Geo controller */
11
namespace Geo\Controller;
12
13
use Geo\Options\ModuleOptions;
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Zend\View\Model\JsonModel;
16
17
/**
18
 * Class IndexController
19
 * @package Geo\Controller
20
 */
21
class IndexController extends AbstractActionController
22
{
23
    /**
24
     * Used geo coder plugin. Copy Geo.options.local.php.dist in your autoload and configure it
25
     *
26
     * @var string $plugin
27
     */
28
    protected $plugin;
29
30
    /**
31
     * Used geo coder server. Copy Geo.options.local.php.dist in your autoload and configure it
32
     *
33
     * @var string $geoCoderUrl
34
     */
35
    protected $geoCoderUrl;
36
37
    public function __construct(ModuleOptions $options) {
38
        $this->plugin = $options->getPlugin();
39
        $this->geoCoderUrl = $options->getGeoCoderUrl();
40
    }
41
42
    /**
43
     * @return JsonModel
44
     */
45
    public function indexAction()
46
    {
47
        $query = $this->params()->fromQuery();
48
49
        switch($this->plugin){
50
            case 'photon':
51
                /* @var Plugin\Photon $geoApi */
52
                $geoApi = $this->getPluginManager()->get('geo/photon');
53
                break;
54
            case 'geo':
55
                /* @var Plugin\Geo $geoApi */
56
                $geoApi = $this->getPluginManager()->get('geo/geo');
57
                break;
58
            default:
59
                throw new \RuntimeException('Invalid geo coder plugin');
60
        }
61
        $result = array();
62
        if (!empty($query['q'])) {
63
            $result = $geoApi($query['q'], $this->geoCoderUrl, $this->params('lang','de'));
64
        }
65
        $viewModel = new JsonModel($result);
66
        return $viewModel;
67
    }
68
}