Completed
Pull Request — master (#68)
by De Cramer
03:16
created

RecordsWindowFactory::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 27
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace eXpansion\Bundle\LocalRecords\Plugins\Gui;
5
use eXpansion\Bundle\Acme\Plugins\Gui\WindowFactory;
6
use eXpansion\Bundle\LocalRecords\Entity\Record;
7
use eXpansion\Framework\Core\Helpers\Time;
8
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
9
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
10
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use FML\Controls\Frame;
13
14
15
/**
16
 * Class RecordsWindowFactory
17
 *
18
 * @package eXpansion\Bundle\LocalRecords\Plugins\Gui;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class RecordsWindowFactory extends WindowFactory
22
{
23
    /** @var Record[] */
24
    protected $recordsData = [];
25
26
    /** @var GridBuilderFactory */
27
    protected $gridBuilderFactory;
28
29
    /** @var DataCollectionFactory */
30
    protected $dataCollectionFactory;
31
32
    /** @var Time */
33
    protected $timeFormatter;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    protected function createContent(ManialinkInterface $manialink)
39
    {
40
        $collection = $this->dataCollectionFactory->create($this->getRecordsData());
41
        $collection->setPageSize(20);
42
43
        $gridBuilder = $this->gridBuilderFactory->create();
44
        $gridBuilder->setManialink($manialink)
45
            ->setDataCollection($collection)
46
            ->setManialinkFactory($this)
47
            ->addTextColumn(
48
                'position',
49
                'expansion_local_records.gui.race.window.column.position',
50
                '1'
51
            )->addTextColumn(
52
                'nickname',
53
                'expansion_local_records.gui.race.window.column.nickname',
54
                '4'
55
            )->addTextColumn(
56
                'login',
57
                'expansion_local_records.gui.race.window.column.login',
58
                '4'
59
            )->addTextColumn(
60
                'score',
61
                'expansion_local_records.gui.race.window.column.score',
62
                '3'
63
            );
64
65
        $manialink->setData('grid', $gridBuilder);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 View Code Duplication
    protected function updateContent(ManialinkInterface $manialink)
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...
72
    {
73
        /** @var Frame $contentFrame */
74
        $contentFrame = $manialink->getContentFrame();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getContentFrame() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

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
        $contentFrame->removeAllChildren();
76
77
        $collection = $this->dataCollectionFactory->create($this->getRecordsData());
78
        $collection->setPageSize(20);
79
80
        /** @var GridBuilder $gridBuilder */
81
        $gridBuilder = $manialink->getData('grid');
82
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    protected function getRecordsData()
89
    {
90
        $recordsData = [];
91
92
        foreach ($this->recordsData as $i => $record) {
93
            $recordsData[] = [
94
                'position' => $i + 1,
95
                'nickname' => $record->getPlayerLogin(),
96
                'login' => $record->getPlayerLogin(),
97
                'score' => $this->timeFormatter->milisecondsToTrackmania($record->getScore(), true),
98
            ];
99
        }
100
101
        return $recordsData;
102
    }
103
104
    /**
105
     * @param GridBuilderFactory $gridBuilderFactory
106
     */
107
    public function setGridBuilderFactory($gridBuilderFactory)
108
    {
109
        $this->gridBuilderFactory = $gridBuilderFactory;
110
    }
111
112
    /**
113
     * @param DataCollectionFactory $dataCollectionFactory
114
     */
115
    public function setDataCollectionFactory($dataCollectionFactory)
116
    {
117
        $this->dataCollectionFactory = $dataCollectionFactory;
118
    }
119
120
    /**
121
     * @param Time $time
122
     */
123
    public function setTimerFormatter(Time $time)
124
    {
125
        $this->timeFormatter = $time;
126
    }
127
128
    /**
129
     * @param Record[] $records
130
     */
131
    public function setRecordsData($records)
132
    {
133
        $this->recordsData = $records;
134
    }
135
}