Completed
Pull Request — master (#8)
by
unknown
10:45
created

Rights::translateCustomFields()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: onysko
5
 * Date: 10.06.2015
6
 * Time: 16:43
7
 */
8
namespace samsoncms\app\security\tab;
9
10
use samson\cms\Navigation;
11
use samsoncms\form\tab\Generic;
12
use samsonframework\core\RenderInterface;
13
use samsonframework\orm\QueryInterface;
14
use samsonframework\orm\Record;
15
16
/**
17
 * SamsonCMS application form tab for group security rights selection
18
 * @package samsoncms\app\security\tab
19
 */
20
class Rights extends Generic
21
{
22
    /** @var string Tab name or identifier */
23
    protected $name = 'Доступные права';
24
25
    /** @var \samsoncms\app\security\Controller */
26
    protected $renderer;
27
28
    /**
29
     * Render checkboxes selection list
30
     * @param array $availableValues Collection of available entities for selection
31
     * @param array $selectedValueIDs Collection of selected entity identifiers
32
     * @param string $controller Select/Un-select controller action route
33
     * @param string $showField Entity field name for showing
34
     * @return string HTML rendered checkboxes list
35
     */
36
    public function renderList(array $availableValues, array $selectedValueIDs, $controller, $showField = 'Description')
37
    {
38
        // Iterate all available values
39
        $html = '';
40
        foreach ($availableValues as $availableValue) {
41
            // Define if this value is selected
42
            $checked = in_array($availableValue->id, $selectedValueIDs) ? 'checked' : '';
43
44
            // Translate all fields
45
            $finishTranslateParts = $this->translateCustomFields($availableValue->$showField);
46
47
            $html .= '<div class="input-container select-checkboxes-list-item">';
48
            // Render checkbox with label
49
            $html .= '<label><input type="checkbox" ' . $checked . ' href="' . url_build($controller, $availableValue->id) . '" value="' . $availableValue->id . '">' . $finishTranslateParts . '</label>';
50
            $html .= '</div>';
51
        }
52
53
        return $html;
54
    }
55
56
    /**
57
     * Function which translated custom fields for application "Rights"
58
     * @param array $inputData
59
     * @return string
60
     */
61
    public function translateCustomFields($inputData)
62
    {
63
        // Search all part this text block
64
        $allTranslateParts = explode("\"", $inputData);
65
        // Remove empty elements
66
        $allTranslateParts = array_filter($allTranslateParts);
67
68
        // First value empty
69
        $finishTranslateParts = '';
70
        // Counter elements in this array
71
        $count = 0;
72
        foreach ($allTranslateParts as $oneTranslateParts) {
73
            // First part (not have quotes)
74
            if ($count == 0) {
75
                $finishTranslateParts .= t($oneTranslateParts, true);
76
                // Last part with quotes
77
            } else {
78
                $finishTranslateParts .= ' "' . t($oneTranslateParts, true) . '"';
79
            }
80
            // Increment count
81
            $count++;
82
        }
83
84
        return $finishTranslateParts;
85
    }
86
87
    /** @inheritdoc */
88
    public function content()
89
    {
90
        // Translate header
91
        $this->name = t($this->name, true);
92
93
        // Access to the application
94
95
        // Render tab content
96
        $content = $this->renderer
97
            ->view('form/tab_item')
98
            ->set($this->renderList(
99
                $this->query->className('right')->exec(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method className() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

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...
100
                $this->query->className('groupright')->cond('GroupID', $this->entity->id)->fields('RightID'),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method className() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

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...
101
                $this->renderer->id() . '/change/' . $this->entity->id
102
            ), 'chbView')
103
            ->output();
104
105
        return $this->renderer
106
            ->view($this->contentView)
107
            ->set($content, 'content')
108
            ->output();
109
    }
110
}
111