Completed
Push — develop ( 0bfa91...da5abe )
by
unknown
17:13 queued 09:01
created

ApplyButtons::getPartial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 2
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
 * @author    [email protected]
9
 */
10
11
namespace Jobs\View\Helper;
12
13
use Zend\View\Helper\AbstractHelper;
14
15
/**
16
 * View helper to display apply buttons
17
 */
18
class ApplyButtons extends AbstractHelper
19
{
20
    
21
    /**
22
     * @var string
23
     */
24
    protected $partial = 'partials/buttons';
25
    
26
    /**
27
     * Renders apply buttons according to passed $options
28
     * Following optional options are recognized:
29
     *      partial:            custom partial script for rendering the buttons (relative to current template script), default: 'partials/buttons'
30
     *      oneClickOnly:       display only one click apply buttons, default: false
31
     *      defaultLabel:       label of default apply button, default: 'Apply now'
32
     *      oneClickLabel:      label of one click apply buttons, default: 'Apply with %s'
33
     *      sendImmediately:    flag whether to sent application immediately, default: false
34
     *
35
     * Usage example with defaults:
36
     * <code>
37
     *      <?=$this->jobApplyButtons($this->applyButtons)?>
38
     * </code>
39
     *
40
     * Usage example with options set explicitly:
41
     * <code>
42
     *      <?=$this->jobApplyButtons($this->applyButtons , [
43
     *          'partial' => 'partials/mybuttons',
44
     *          'oneClickOnly' => true,
45
     *          'defaultLabel' => $this->translate('Send application'),
46
     *          'oneClickLabel' => $this->translate('Send application with my %s social profile'),
47
     *          'sendImmediately' => true,
48
     *      ])?>
49
     * </code>
50
     * @param array $data
51
     * @param array $options
52
     * @return string
53
     */
54
    public function __invoke(array $data, array $options = [])
55
    {
56
        // check data
57
        if (!isset($data['uri']) || !isset($data['oneClickProfiles']) || !isset($data['applyId']))
58
        {
59
            throw new \InvalidArgumentException('Invalid data passed');
60
        }
61
        
62
        $variables = [
63
            'default' => null,
64
            'oneClick' => [],
65
        ];
66
        $options = array_merge([
67
            'partial' => $this->partial,
68
            'oneClickOnly' => false,
69
            'defaultLabel' => null,
70
            'oneClickLabel' => null,
71
            'sendImmediately' => false
72
        ], $options);
73
        $view = $this->view;
74
		$currentTemplate = $view->viewModel()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method viewModel() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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...
75
            ->getCurrent()
76
            ->getTemplate();
77
        $partial = dirname($currentTemplate) . '/' . $options['partial'];
78
        
79
        if (!$options['oneClickOnly'] && $data['uri']) {
80
            $variables['default'] = [
81
                'label' => $options['defaultLabel'] ?: $view->translate('Apply now'),
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
                'url' => $data['uri']
83
            ];
84
        }
85
        
86
        if ($data['oneClickProfiles']) {
87
            $label = $options['oneClickLabel'] ?: $view->translate('Apply with %s');
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            
89
            foreach ($data['oneClickProfiles'] as $network) {
90
				$variables['oneClick'][] = [
91
                    'label' => sprintf($label, $network),
92
                    'url' => $view->url('lang/apply-one-click', ['applyId' => $data['applyId'], 'network' => $network, 'immediately' => $options['sendImmediately'] ?: null], ['force_canonical' => true]),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method url() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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...
93
				    'network' => $network
94
                ];
95
            }
96
        }
97
        
98
        return $view->partial($partial, $variables);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method partial() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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...
99
    }
100
    
101
	/**
102
	 * @return string
103
	 */
104
	public function getPartial()
105
	{
106
		return $this->partial;
107
	}
108
109
	/**
110
	 * @param string $partial
111
	 * @return ApplyButtons
112
	 */
113
	public function setPartial($partial)
114
	{
115
		$this->partial = $partial;
116
		
117
		return $this;
118
	}
119
}
120