Completed
Push — develop ( 69bf64...c61561 )
by
unknown
17:17 queued 09:01
created

ManageController::saveAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** ActionController of Core */
11
namespace Cv\Controller;
12
13
use Zend\Mvc\Controller\AbstractActionController;
14
use Zend\View\Model\JsonModel;
15
use Core\Form\SummaryFormInterface;
16
17
/**
18
 * Main Action Controller for the application.
19
 * Responsible for displaying the home site.
20
 *
21
 */
22
class ManageController extends AbstractActionController
23
{
24
25
    /**
26
     * attaches further Listeners for generating / processing the output
27
     * @return $this
28
     */
29 View Code Duplication
    public function attachDefaultListeners()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
30
    {
31
        parent::attachDefaultListeners();
32
        $serviceLocator  = $this->serviceLocator;
33
        $defaultServices = $serviceLocator->get('DefaultListeners');
34
        $events          = $this->getEventManager();
35
        $events->attach($defaultServices);
0 ignored issues
show
Documentation introduced by
$defaultServices is of type object|array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
        return $this;
37
    }
38
39
    /**
40
     * Home site
41
     *
42
     */
43
    public function indexAction()
44
    {
45
    }
46
    
47
    public function formAction()
48
    {
49
        $serviceLocator = $this->serviceLocator;
50
        $repositories = $serviceLocator->get('repositories');
51
        /* @var $cvRepository \Cv\Repository\Cv */
52
        $cvRepository = $repositories->get('Cv/Cv');
53
        $user = $this->auth()->getUser();
0 ignored issues
show
Documentation Bug introduced by
The method auth does not exist on object<Cv\Controller\ManageController>? 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...
54
        /* @var $cv \Cv\Entity\Cv */
55
        $cv = $cvRepository->findDraft($user);
56
        
57
        if (empty($cv)) {
58
            // create draft CV
59
            $cv = $cvRepository->create();
60
            $cv->setIsDraft(true);
61
            $cv->setContact($user->getInfo());
62
            $cv->setUser($user);
63
            $repositories->store($cv);
64
        }
65
        
66
        /* @var $container \Core\Form\Container */
67
        $container = $serviceLocator->get('FormElementManager')
68
            ->get('CvContainer')
69
            ->setEntity($cv);
70
        
71
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
72
            $params = $this->params();
73
            $form = $container->getForm($params->fromQuery('form'));
74
            
75
            if ($form) {
76
                $form->setData(array_merge(
77
                    $params->fromPost(),
78
                    $params->fromFiles()
79
                ));
80
                
81
                if (!$form->isValid()) {
82
                    return new JsonModel([
83
                        'valid' => false,
84
                        'errors' => $form->getMessages()
85
                    ]);
86
                }
87
                
88
                $repositories->store($cv);
89
                
90
                if ($form instanceof SummaryFormInterface) {
91
                    $form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY);
92
                    $viewHelper = 'summaryform';
93
                } else {
94
                    $viewHelper = 'form';
95
                }
96
                
97
                // render form
98
                $content = $serviceLocator->get('ViewHelperManager')
99
                    ->get($viewHelper)
100
                    ->__invoke($form);
101
                
102
                return new JsonModel([
103
                    'valid' => true,
104
                    'content' => $content
105
                ]);
106
            } elseif (($action = $params->fromQuery('action')) !== null) {
107
                return new JsonModel($container->executeAction($action, $params->fromPost()));
108
            }
109
        }
110
        
111
        return [
112
            'container' => $container
113
        ];
114
    }
115
}
116