ComposerJsonValidator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 22
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 2
1
<?php
2
3
/*
4
 * This file is part of `Composer as a service`.
5
 *
6
 * (c) Pascal Borreli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ayaline\Bundle\ComposerBundle\Validator\Constraints;
13
14
use Composer\Json\JsonFile;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\ConstraintValidator;
17
18
/**
19
 * @author Abdellatif Ait boudad <[email protected]>
20
 */
21
class ComposerJsonValidator extends ConstraintValidator
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function validate($value, Constraint $constraint)
27
    {
28
        $tempFile = tempnam(sys_get_temp_dir(), 'composer');
29
        file_put_contents($tempFile, $value);
30
31
        try {
32
            $jsonFile = new JsonFile($tempFile);
33
            $jsonFile->validateSchema(JsonFile::LAX_SCHEMA);
34
            unlink($tempFile);
35
        } catch (\Exception $exception) {
36
            unlink($tempFile);
37
            $from = [$tempFile];
38
            $to = ['composer.json'];
39
            $this->context->addViolation(str_replace($from, $to, $exception->getMessage()));
40
        }
41
    }
42
}
43