Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Seeder
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
8 / 8
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDatabase
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSilent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isSilent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
n/a
0 / 0
n/a
0 / 0
0
 call
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
 runSeed
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 runCli
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 isCli
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Database Extra Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Database\Extra;
11
12use Framework\CLI\CLI;
13use Framework\Database\Database;
14
15/**
16 * Class Seeder.
17 *
18 * @package database-extra
19 */
20abstract class Seeder
21{
22    protected Database $database;
23    protected bool $silent;
24
25    /**
26     * Seeder constructor.
27     *
28     * @param Database $database
29     */
30    public function __construct(Database $database)
31    {
32        $this->database = $database;
33    }
34
35    public function getDatabase() : Database
36    {
37        return $this->database;
38    }
39
40    public function setSilent(bool $isSilent = true) : static
41    {
42        $this->silent = $isSilent;
43        return $this;
44    }
45
46    public function isSilent() : bool
47    {
48        return $this->silent ?? false;
49    }
50
51    /**
52     * Run the Seeder.
53     */
54    abstract public function run() : void;
55
56    /**
57     * Call seeders to run.
58     *
59     * @param array<int,Seeder|string>|Seeder|string $seeds
60     */
61    protected function call(array | Seeder | string $seeds) : void
62    {
63        if (\is_string($seeds)) {
64            $seeds = [new $seeds($this->getDatabase())];
65        } elseif (\is_array($seeds)) {
66            foreach ($seeds as &$seed) {
67                if (\is_string($seed)) {
68                    $seed = new $seed($this->getDatabase());
69                }
70            }
71            unset($seed);
72        }
73        $seeds = \is_array($seeds) ? $seeds : [$seeds];
74        foreach ($seeds as $seed) {
75            $this->runSeed($seed); // @phpstan-ignore-line
76        }
77    }
78
79    protected function runSeed(Seeder $seed) : void
80    {
81        ! $this->isCli() || $this->isSilent()
82            ? $seed->run()
83            : $seed->runCli($seed);
84    }
85
86    protected function runCli(Seeder $seed) : void
87    {
88        $class = CLI::style($seed::class, CLI::FG_GREEN);
89        CLI::liveLine('- Seeding ' . $class . '...');
90        $runtime = \microtime(true);
91        $seed->run();
92        $runtime = \microtime(true) - $runtime;
93        $runtime = \round($runtime, 6);
94        $runtime = CLI::style((string) $runtime, CLI::FG_YELLOW);
95        CLI::liveLine(
96            '- Seeded ' . $class . ' in ' . $runtime . ' seconds.',
97            true
98        );
99    }
100
101    protected function isCli() : bool
102    {
103        return \PHP_SAPI === 'cli' || \defined('STDIN');
104    }
105}