Completed
Push — master ( 68693d...b79797 )
by Adam
12:25
created

PackageFormBuilder::buildForm()   B

↳ Parent: PackageFormBuilder

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 44
Code Lines 26

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 26
nc 2
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
namespace WellCommerce\Bundle\AppBundle\Form\Admin;
13
14
use Packagist\Api\Result\Package\Version;
15
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
16
use WellCommerce\Bundle\CoreBundle\Helper\Package\PackageHelperInterface;
17
use WellCommerce\Component\Form\Elements\FormInterface;
18
19
/**
20
 * Class PackageFormBuilder
21
 *
22
 * @author Adam Piotrowski <[email protected]>
23
 */
24
class PackageFormBuilder extends AbstractFormBuilder
25
{
26
    public function getAlias(): string
27
    {
28
        return 'admin.package';
29
    }
30
    
31
    public function buildForm(FormInterface $packageForm)
32
    {
33
        $router    = $this->getRouterHelper();
34
        $package   = $this->getRequestHelper()->getAttributesBagParam('id');
35
        $operation = $this->getRequestHelper()->getAttributesBagParam('operation');
36
        $versions  = $this->getPackageVersions($package);
37
        
38
        $packageData = $packageForm->addChild($this->getElement('nested_fieldset', [
39
            'name'  => 'package_data',
40
            'label' => 'package.fieldset.information',
41
        ]));
42
        
43
        foreach ($versions as $version) {
44
            
45
            $license = implode('<br />', $version['license']);
46
            
47
            $packageData->addChild($this->getElement('static_text', [
48
                'text' => "
49
                    <table>
50
                        <tr><td><strong>Release date:</strong></td><td>{$version['date']}</td></tr>
51
                        <tr><td><strong>Description:</strong></td><td>{$version['description']}</td></tr>
52
                        <tr><td><strong>License:</strong></td><td>{$license}</td></tr>
53
                    </table>
54
                ",
55
            ]));
56
        }
57
        
58
        $packageRequiredData = $packageForm->addChild($this->getElement('nested_fieldset', [
59
            'name'  => 'progress_data',
60
            'label' => 'package.fieldset.progress',
61
        ]));
62
        
63
        $packageRequiredData->addChild($this->getElement('console_output', [
64
            'name'         => 'console_output',
65
            'label'        => 'package.label.console_output',
66
            'button_label' => 'package.button.' . $operation,
67
            'run_url'  => $router->generateUrl(
68
                'admin.package.run', [
69
                    'id'        => $package,
70
                    'operation' => $operation,
71
                ]
72
            ),
73
        ]));
74
    }
75
    
76
    /**
77
     * Returns version information for package
78
     *
79
     * @param $id
80
     *
81
     * @return array
82
     */
83
    protected function getPackageVersions($id)
84
    {
85
        $localPackage  = $this->get('package.repository')->findOneById($id);
86
        $remotePackage = $this->get('package.helper')->getPackage($localPackage->getFullName());
87
        $versions      = $remotePackage->getVersions();
88
        $result        = [];
89
        
90
        foreach ($versions as $version) {
91
            $this->getPackageInfo($version, $result);
92
        }
93
        
94
        return $result;
95
    }
96
    
97
    /**
98
     * Adds information about single version to result array
99
     *
100
     * @param Version $version
101
     * @param array   $result
102
     */
103
    protected function getPackageInfo(Version $version, &$result)
104
    {
105
        $date           = new \DateTime($version->getTime());
106
        $packageVersion = $version->getVersion();
107
        if ($packageVersion === PackageHelperInterface::DEFAULT_BRANCH_VERSION) {
108
            $result[$packageVersion] = [
109
                'version'     => $packageVersion,
110
                'date'        => $date->format('Y-m-d H:i:s'),
111
                'authors'     => $version->getAuthors(),
112
                'description' => $version->getDescription(),
113
                'homepage'    => $version->getHomepage(),
114
                'license'     => $version->getLicense(),
115
            ];
116
        }
117
    }
118
}
119
120