• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

selamiphp / console / 51

pending completion
51

push

travis-ci-com

mkorkmaz
fix: cs

1 of 1 new or added line in 1 file covered. (100.0%)

34 of 42 relevant lines covered (80.95%)

6.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.95
/src/Console/ApplicationFactory.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Selami\Console;
5

6
use Symfony\Component\Console\Application;
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Selami\Stdlib\Resolver;
10
use ReflectionClass;
11
use Selami\Console\Exception\DependencyNotFoundException;
12
use Selami\Stdlib\Exception\ClassOrMethodCouldNotBeFound;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14

15
class ApplicationFactory
16
{
17
    public static function makeApplication(
18
        ContainerInterface $container,
19
        string $name = 'ConsoleApp',
20
        string $version = '0.0.1'
21
    ) : Application {
22
        /**
23
         * @var array $commands
24
         */
25
        $commands = $container->get('commands');
9✔
26
        $cli = new Application($name, $version);
9✔
27
        $consoleApplicationArguments = [
9✔
28
            'name' => $name,
9✔
29
            'version' => $version
9✔
30
        ];
9✔
31
        foreach ($commands as $command) {
9✔
32
            $controllerConstructorArguments = self::getControllerConstructorArguments($command);
9✔
33
            $arguments = [];
9✔
34
            foreach ($controllerConstructorArguments as $argumentName => $argumentType) {
9✔
35
                $arguments[] = self::getArgument(
9✔
36
                    $container,
9✔
37
                    $consoleApplicationArguments,
9✔
38
                    $argumentName,
9✔
39
                    $argumentType
9✔
40
                );
9✔
41
            }
42
            $reflectionClass = new ReflectionClass($command);
9✔
43
            /**
44
             * @var Command $autoWiredCommand
45
             */
46
            $autoWiredCommand = $reflectionClass->newInstanceArgs($arguments);
9✔
47
            $cli->add($autoWiredCommand);
9✔
48
        }
49
        return $cli;
3✔
50
    }
51

52
    private static function getControllerConstructorArguments($command): ?array
53
    {
54
        try {
55
            return Resolver::getParameterHints($command, '__construct');
9✔
56
        } catch (ClassOrMethodCouldNotBeFound|ServiceNotFoundException $e) {
×
57
            throw new DependencyNotFoundException(
×
58
                sprintf(
×
59
                    '%s when calling command: %s',
×
60
                    $e->getMessage(),
×
61
                    $command
×
62
                )
×
63
            );
×
64
        }
65
    }
66

67
    private static function getArgument(
68
        ContainerInterface $container,
69
        array $consoleApplicationArguments,
70
        string $argumentName,
71
        string $argumentType
72
    ) {
73

74
        if (array_key_exists($argumentName, $consoleApplicationArguments)) {
9✔
75
            return $consoleApplicationArguments[$argumentName];
9✔
76
        }
77
        if ($argumentType === Resolver::ARRAY && $container->has($argumentName) === false) {
9✔
78
            throw new DependencyNotFoundException(
3✔
79
                sprintf('Container does not have an item for array: %s', $argumentName)
3✔
80
            );
3✔
81
        }
82
        if ($argumentType === Resolver::ARRAY) {
9✔
83
            return $container->get($argumentName);
9✔
84
        }
85
        try {
86
            return $container->get($argumentType);
9✔
87
        } catch (ServiceNotFoundException) {
3✔
88
            throw new DependencyNotFoundException(
3✔
89
                sprintf('Container does not have an item service: %s', $argumentType)
3✔
90
            );
3✔
91
        }
92
    }
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc