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

aplus-framework / database-extra / 16536129688

23 Aug 2024 09:59PM UTC coverage: 100.0%. Remained the same
16536129688

push

github

natanfelles
Upgrade coding standard

150 of 150 relevant lines covered (100.0%)

3.61 hits per line

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

100.0
/src/Seeder.php
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
 */
10
namespace Framework\Database\Extra;
11

12
use Framework\CLI\CLI;
13
use Framework\Database\Database;
14

15
/**
16
 * Class Seeder.
17
 *
18
 * @package database-extra
19
 */
20
abstract 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;
3✔
33
    }
34

35
    public function getDatabase() : Database
36
    {
37
        return $this->database;
3✔
38
    }
39

40
    public function setSilent(bool $isSilent = true) : static
41
    {
42
        $this->silent = $isSilent;
1✔
43
        return $this;
1✔
44
    }
45

46
    public function isSilent() : bool
47
    {
48
        return $this->silent ?? false;
3✔
49
    }
50

51
    /**
52
     * Run the Seeder.
53
     */
54
    abstract public function run() : void;
55

56
    /**
57
     * Call seeders to run.
58
     *
59
     * @param Seeder|array<int,Seeder|string>|string $seeds
60
     */
61
    protected function call(Seeder | array | string $seeds) : void
62
    {
63
        if (\is_string($seeds)) {
3✔
64
            $seeds = [new $seeds($this->getDatabase())];
1✔
65
        } elseif (\is_array($seeds)) {
3✔
66
            foreach ($seeds as &$seed) {
3✔
67
                if (\is_string($seed)) {
3✔
68
                    $seed = new $seed($this->getDatabase());
3✔
69
                }
70
            }
71
            unset($seed);
3✔
72
        }
73
        $seeds = \is_array($seeds) ? $seeds : [$seeds];
3✔
74
        foreach ($seeds as $seed) {
3✔
75
            $this->runSeed($seed); // @phpstan-ignore-line
3✔
76
        }
77
    }
78

79
    protected function runSeed(Seeder $seed) : void
80
    {
81
        !$this->isCli() || $this->isSilent()
3✔
82
            ? $seed->run()
1✔
83
            : $seed->runCli($seed);
2✔
84
    }
85

86
    protected function runCli(Seeder $seed) : void
87
    {
88
        $class = CLI::style($seed::class, 'green');
2✔
89
        CLI::liveLine('- Seeding ' . $class . '...');
2✔
90
        $runtime = \microtime(true);
2✔
91
        $seed->run();
2✔
92
        $runtime = \microtime(true) - $runtime;
2✔
93
        $runtime = \round($runtime, 6);
2✔
94
        $runtime = CLI::style((string) $runtime, 'yellow');
2✔
95
        CLI::liveLine(
2✔
96
            '- Seeded ' . $class . ' in ' . $runtime . ' seconds.',
2✔
97
            true
2✔
98
        );
2✔
99
    }
100

101
    protected function isCli() : bool
102
    {
103
        return \PHP_SAPI === 'cli' || \defined('STDIN');
3✔
104
    }
105
}
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

© 2026 Coveralls, Inc