<?php

namespace Dipper\Console\Commands;

use Dipper\Console\Finder;
use Dipper\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

/**
 * @author HollyTeng <n.haoyuan@gmail.com>
 */
class ServiceListCommand extends SymfonyCommand
{
    use Finder;
    use Command;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'list:services';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'List the services.';

    /**
     * Execute the console command.
     *
     * @return bool|null
     */
    public function fire()
    {
        foreach ($this->listServices($this->argument('domain')) as $domain => $services) {
            $this->comment("\n$domain\n");
            $services = array_map(function ($service) {
                return [$service->title, $service->domain->name, $service->file, $service->relativePath];
            }, $services->all());
            $this->table(['Service', 'Domain', 'File', 'Path'], $services);
        }
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['domain', InputArgument::OPTIONAL, 'The domain to list the services of.'],
        ];
    }
}
