File size: 1,133 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

declare(strict_types=1);

namespace Plugin\Domain\Services;

use Plugin\Domain\Context;
use Plugin\Domain\Exceptions\InvalidPluginComposerJsonFileException;
use Plugin\Domain\Exceptions\InvalidPluginException;
use Plugin\Domain\Exceptions\PluginComposerFileNotFoundException;
use ZipArchive;

class PluginValidationService
{
    /**
     * @param string $zipFilePath
     * @return Context
     * @throws InvalidPluginException
     * @throws PluginComposerFileNotFoundException
     * @throws InvalidPluginComposerJsonFileException
     */
    public function validateZipFile(string $zipFilePath): Context
    {
        $zip = new ZipArchive();
        $openResp = $zip->open($zipFilePath, ZipArchive::RDONLY);

        if ($openResp !== true) {
            throw new InvalidPluginException(
                "Failed open zip archive with following code: " . $openResp
            );
        }

        $jsonFileContent = $zip->getFromName('composer.json');
        if ($jsonFileContent === false) {
            throw new PluginComposerFileNotFoundException();
        }

        return new Context($jsonFileContent);
    }
}