Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
20 / 20
SmalldbDefinitionBag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
20 / 20
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getDefinition
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 getAllDefinitions
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAllAliases
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAllMachineTypes
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 addDefinition
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 addAlias
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
<?php declare(strict_types = 1);
/*
 * Copyright (c) 2019, Josef Kufner  <josef@kufner.cz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
namespace Smalldb\StateMachine;
use Smalldb\StateMachine\Definition\StateMachineDefinition;
class SmalldbDefinitionBag implements SmalldbDefinitionBagInterface
{
    /** @var StateMachineDefinition[] */
    private array $definitionBag = [];
    /** @var string[] */
    private array $aliases = [];
    public function __construct()
    {
    }
    public function getDefinition(string $machineType): StateMachineDefinition
    {
        if (isset($this->definitionBag[$machineType])) {
            return $this->definitionBag[$machineType];
        } else if (isset($this->aliases[$machineType])) {
            return $this->definitionBag[$this->aliases[$machineType]];
        } else {
            throw new InvalidArgumentException("Undefined machine type: $machineType");
        }
    }
    /**
     * @return StateMachineDefinition[]
     */
    public function getAllDefinitions(): array
    {
        return $this->definitionBag;
    }
    /**
     * @return string[]
     */
    public function getAllAliases(): array
    {
        return $this->aliases;
    }
    /**
     * @return string[]
     */
    public function getAllMachineTypes(): array
    {
        return array_keys($this->definitionBag);
    }
    public function addDefinition(StateMachineDefinition $definition): string
    {
        $machineType = $definition->getMachineType();
        if (isset($this->definitionBag[$machineType])) {
            throw new InvalidArgumentException("Duplicate state machine type: $machineType");
        } else {
            $this->definitionBag[$machineType] = $definition;
            return $machineType;
        }
    }
    public function addAlias(string $alias, string $machineType)
    {
        if (isset($this->aliases[$alias])) {
            throw new InvalidArgumentException("Duplicate state machine alias: $alias");
        } else if (isset($this->definitionBag[$machineType])) {
            $this->aliases[$alias] = $machineType;
        } else {
            throw new InvalidArgumentException("Undefined machine type: $machineType");
        }
    }
}