Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
41 / 41
AbstractSession
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
10 / 10
20
100.00% covered (success)
100.00%
41 / 41
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 addDataMapper
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 addGlobalDataMapper
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 addKeyDataMapper
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 set
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 mapValueToSessionData
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 get
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 mapValueFromSessionData
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
10 / 10
 anonymous function
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 clear
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php declare(strict_types = 1);
/**
 * Copyright (c) 2016 Holger Woltersdorf & Contributors
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
namespace IceHawk\Session;
use IceHawk\Session\Interfaces\MapsSessionData;
/**
 * Class AbstractSession
 * @package IceHawk\Session
 */
abstract class AbstractSession
{
    /** @var array */
    private $sessionData;
    /** @var array|MapsSessionData[] */
    private $keyDataMappers;
    /** @var array|MapsSessionData[] */
    private $globalDataMappers;
    public function __construct( array &$sessionData )
    {
        $this->sessionData       = &$sessionData;
        $this->keyDataMappers    = [];
        $this->globalDataMappers = [];
    }
    final public function addDataMapper( MapsSessionData $dataMapper, array $keys = [] )
    {
        if ( empty($keys) )
        {
            $this->addGlobalDataMapper( $dataMapper );
        }
        else
        {
            foreach ( array_unique( $keys ) as $key )
            {
                $this->addKeyDataMapper( $dataMapper, $key );
            }
        }
    }
    private function addGlobalDataMapper( MapsSessionData $dataMapper )
    {
        if ( !in_array( $dataMapper, $this->globalDataMappers ) )
        {
            $this->globalDataMappers[] = $dataMapper;
        }
    }
    private function addKeyDataMapper( MapsSessionData $dataMapper, string $key )
    {
        if ( isset($this->keyDataMappers[ $key ]) )
        {
            if ( !in_array( $dataMapper, $this->keyDataMappers[ $key ] ) )
            {
                $this->keyDataMappers[ $key ][] = $dataMapper;
            }
        }
        else
        {
            $this->keyDataMappers[ $key ] = [$dataMapper];
        }
    }
    final protected function set( string $key, $value )
    {
        $this->sessionData[ $key ] = $this->mapValueToSessionData( $key, $value );
    }
    private function mapValueToSessionData( string $key, $value )
    {
        $keyDataMappers = $this->keyDataMappers[ $key ] ?? [];
        /** @var MapsSessionData $keyDataMapper */
        foreach ( $keyDataMappers as $keyDataMapper )
        {
            $value = $keyDataMapper->toSessionData( $value );
        }
        foreach ( $this->globalDataMappers as $globalDataMapper )
        {
            $value = $globalDataMapper->toSessionData( $value );
        }
        return $value;
    }
    final protected function get( string $key )
    {
        return $this->mapValueFromSessionData( $key );
    }
    private function mapValueFromSessionData( string $key )
    {
        if ( $this->isset( $key ) )
        {
            $value             = $this->sessionData[ $key ];
            $globalDataMappers = array_reverse( $this->globalDataMappers );
            $keyDataMappers    = array_reverse( $this->keyDataMappers[ $key ] ?? [] );
            /** @var MapsSessionData $globalDataMapper */
            foreach ( $globalDataMappers as $globalDataMapper )
            {
                $value = $globalDataMapper->fromSessionData( $value );
            }
            /** @var MapsSessionData $keyDataMapper */
            foreach ( $keyDataMappers as $keyDataMapper )
            {
                $value = $keyDataMapper->fromSessionData( $value );
            }
            return $value;
        }
        return null;
    }
    final protected function isset( string $key ) : bool
    {
        return isset($this->sessionData[ $key ]);
    }
    final protected function unset( string $key )
    {
        unset($this->sessionData[ $key ]);
    }
    public function clear()
    {
        $this->sessionData = [];
    }
}