ModelManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 13 3
1
<?php
2
3
/*
4
 * This file is part of the Scrawler package.
5
 *
6
 * (c) Pranjal Pandey <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Scrawler\Arca\Manager;
15
16
use DI\Container;
17
use Scrawler\Arca\Config;
18
use Scrawler\Arca\Model;
19
20
/**
21
 * Class for initializing and managing models.
22
 */
23
final class ModelManager
24
{
25
    public function __construct(
26
        private readonly Container $container,
27
        private readonly Config $config,
28
    ) {
29
    }
30
31
    /**
32
     * Create a new model instance.
33
     *
34
     * @throws \DI\DependencyException
35
     * @throws \DI\NotFoundException
36
     */
37
    public function create(string $name): Model
38
    {
39
        // Try to load specific model class first if modelNamespace is not empty
40
        $namespace = $this->config->getModelNamespace();
41
        if (!empty($namespace)) {
42
            $modelClass = $namespace.ucfirst($name);
43
            if (class_exists($modelClass)) {
44
                return $this->container->make($modelClass);
45
            }
46
        }
47
48
        // Fallback to generic model with table name
49
        return $this->container->make(Model::class, ['table' => $name]);
50
    }
51
}
52