Completed
Branch develop (bc6cb3)
by Carsten
37:10 queued 26:53
created

TemplateController::getTemplateFields()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 79
Code Lines 58

Duplication

Lines 6
Ratio 7.59 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 79
rs 6.0573
cc 8
eloc 58
nc 24
nop 2

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
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @author [email protected]
8
 * @license   MIT
9
 */
10
11
namespace Jobs\Controller;
12
13
use Jobs\Form\JobDescriptionTemplate;
14
use Jobs\Repository;
15
use Zend\Mvc\Controller\AbstractActionController;
16
use Zend\View\Model\ViewModel;
17
use Zend\View\Model\JsonModel;
18
use Zend\Stdlib\AbstractOptions;
19
20
/**
21
 * Handles rendering the job in formular and in preview mode
22
 *
23
 * Class TemplateController
24
 * @package Jobs\Controller
25
 */
26
class TemplateController extends AbstractActionController
27
{
28
29
    /**
30
     * @var Repository\Job $jobRepository
31
     */
32
    private $jobRepository;
33
34
    /**
35
     * @var AbstractOptions
36
     */
37
    protected $config;
38
39
    public function __construct(Repository\Job $jobRepository, AbstractOptions $config)
40
    {
41
        $this->jobRepository = $jobRepository;
42
        $this->config = $config;
43
    }
44
45
    /**
46
     * Handles the job opening template in preview mode
47
     *
48
     * @return ViewModel
49
     * @throws \RuntimeException
50
     */
51
    public function viewAction()
52
    {
53
        $id = $this->params()->fromQuery('id');
54
        $job = $this->jobRepository->find($id);
55
        $services             = $this->getServiceLocator();
56
        $mvcEvent             = $this->getEvent();
57
        $applicationViewModel = $mvcEvent->getViewModel();
58
59
        $isAdmin=$this->auth()->isAdmin();
0 ignored issues
show
Documentation Bug introduced by
The method auth does not exist on object<Jobs\Controller\TemplateController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Unused Code introduced by
$isAdmin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
        $model = $services->get('Jobs/viewModelTemplateFilter')->__invoke($job);
62
63
        # @todo make this working for anonymous users
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
//        if ($job->status != 'active' && !$job->getPermissions()->isChangeGranted($this->auth()->getUser()) && ! $isAdmin) {
65
//            $this->response->setStatusCode(404);
66
//            $model->setVariable('message','job is not available');
67
//        } else {
68
            $applicationViewModel->setTemplate('iframe/iFrameInjection');
69
//        }
70
        return $model;
71
    }
72
73
    /**
74
     * Handles the job opening template in formular mode
75
     *
76
     * @return ViewModel
77
     */
78
    protected function editTemplateAction()
79
    {
80
        $id = $this->params('id');
81
        $formIdentifier=$this->params()->fromQuery('form');
82
        $job = $this->jobRepository->find($id);
83
84
        $request              = $this->getRequest();
85
        $isAjax               = $request->isXmlHttpRequest();
86
        $services             = $this->getServiceLocator();
87
        $viewHelperManager    = $services->get('ViewHelperManager');
88
        $mvcEvent             = $this->getEvent();
89
        $applicationViewModel = $mvcEvent->getViewModel();
90
        $forms                = $services->get('FormElementManager');
91
        /** @var \Jobs\Form\JobDescriptionTemplate $formTemplate */
92
        $formTemplate         = $forms->get(
93
            'Jobs/Description/Template',
94
            array(
95
            'mode' => $job->id ? 'edit' : 'new'
96
            )
97
        );
98
99
        $formTemplate->setParam('id', $job->id);
100
        $formTemplate->setParam('applyId', $job->applyId);
101
        $formTemplate->setEntity($job);
102
103
        if (isset($formIdentifier) && $request->isPost()) {
104
            // at this point the form get instanciated and immediately accumulated
105
            $instanceForm = $formTemplate->get($formIdentifier);
106
            if (!isset($instanceForm)) {
107
                throw new \RuntimeException('No form found for "' . $formIdentifier . '"');
108
            }
109
            // the id is part of the postData, but it never should be altered
110
            $postData = $request->getPost();
111
            unset($postData['id']);
112
            unset($postData['applyId']);
113
            $instanceForm->setData($postData);
114
            if ($instanceForm->isValid()) {
115
                $this->getServiceLocator()->get('repositories')->persist($job);
116
            }
117
        }
118
119
        $model = $services->get('Jobs/viewModelTemplateFilter')->__invoke($formTemplate);
120
121
        if (!$isAjax) {
122
            $basePath   = $viewHelperManager->get('basepath');
123
            $headScript = $viewHelperManager->get('headscript');
124
            $headScript->appendFile($basePath->__invoke('/Core/js/core.forms.js'));
125
        } else {
126
            return new JsonModel(array('valid' => true));
127
        }
128
        $applicationViewModel->setTemplate('iframe/iFrameInjection');
129
        return $model;
130
    }
131
132
    /**
133
     * Gets the organization logo. If no logo exists, take a predefined one
134
     *
135
     * @param \Organizations\Entity\Organization $organization
136
     * @return String
137
     */
138
    private function getOrganizationLogo(\Organizations\Entity\Organization $organization)
139
    {
140
        if (isset($organization) && isset($organization->image) && $organization->image->uri) {
0 ignored issues
show
Documentation introduced by
The property $image is declared protected in Organizations\Entity\Organization. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property uri does not exist on object<Organizations\Entity\OrganizationImage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
            return ($organization->image->uri);
0 ignored issues
show
Documentation introduced by
The property $image is declared protected in Organizations\Entity\Organization. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property uri does not exist on object<Organizations\Entity\OrganizationImage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
        } else {
143
            /** @var \Zend\ServiceManager\ServiceManager $serviceLocator */
144
            return $this->config->default_logo;
145
        }
146
    }
147
}
148