Passed
Push — master ( 655177...3296ef )
by Anthony
11:29
created

Version::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.8666
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
class Version
8
{
9
    /**
10
     * @var EntityManagerInterface
11
     */
12
    private $em;
13
14
    /**
15
     * Version constructor.
16
     * @param EntityManagerInterface $em
17
     */
18
    public function __construct(EntityManagerInterface $em)
19
    {
20
        $this->em = $em;
21
    }
22
23
    /**
24
     * @return mixed|null
25
     */
26
    private function getComposerLockJson()
27
    {
28
        $composer_lock = file_get_contents('../composer.lock');
29
30
        if ($composer_lock) {
31
            return json_decode($composer_lock, true);
32
        }
33
34
        return null;
35
    }
36
37
    /**
38
     * @param $package_name
39
     * @return mixed|null
40
     */
41
    private function getPackage($package_name)
42
    {
43
        $composer_lock = $this->getComposerLockJson();
44
        if ($composer_lock) {
45
            $packages = $composer_lock["packages"];
46
            $key = array_search($package_name, array_column($packages, 'name'));
47
48
            if ($key) {
49
                return $packages[$key];
50
            }
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * @param $package_name
58
     * @return mixed|null
59
     */
60
    public function getVersion($package_name)
61
    {
62
        return $this->getPackage($package_name) ? $this->getPackage($package_name)["version"] : null;
63
    }
64
65
    /**
66
     * @param $package_name
67
     * @return mixed|null
68
     */
69
    public function getVersionDate($package_name)
70
    {
71
        $string_date = $this->getPackage($package_name) ? explode("T", $this->getPackage($package_name)["time"])[0] : null;
72
        $version_date = null;
73
74
        if ($string_date) {
75
            $version_date = new \DateTime($string_date);
76
        }
77
78
        return $version_date;
79
    }
80
81
    /**
82
     * @param $package_name
83
     */
84
    public function save($package_name)
85
    {
86
        $version = $this->em->getRepository(\PiouPiou\RibsAdminBundle\Entity\Version::class)->findOneBy(["package_name" => $package_name]);
87
88
        if (!$version) {
89
            $version = new \PiouPiou\RibsAdminBundle\Entity\Version();
90
            $version->setProjectName($package_name);
91
            $version->setPackageName($package_name);
92
            $version->setProjectUrl($package_name);
93
            $version->setCheckVersionUrl($package_name);
94
        }
95
96
        $version->setVersion($this->getVersion($package_name));
0 ignored issues
show
Bug introduced by
It seems like $this->getVersion($package_name) can also be of type null; however, parameter $version of PiouPiou\RibsAdminBundle...y\Version::setVersion() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $version->setVersion(/** @scrutinizer ignore-type */ $this->getVersion($package_name));
Loading history...
97
        $version->setVersionDate($this->getVersionDate($package_name));
0 ignored issues
show
Bug introduced by
It seems like $this->getVersionDate($package_name) can also be of type null; however, parameter $version_date of PiouPiou\RibsAdminBundle...rsion::setVersionDate() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $version->setVersionDate(/** @scrutinizer ignore-type */ $this->getVersionDate($package_name));
Loading history...
98
        $version->setLastCheck(new \DateTime());
99
100
        $this->em->persist($version);
101
        $this->em->flush();
102
    }
103
}
104