<commit_before><?php
/*
 * This file is part of the PommProject/Foundation package.
 *
 * (c) 2014 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PommProject\Foundation;

use PommProject\Foundation\Exception\FoundationException;
use PommProject\Foundation\Session\SessionBuilder as VanillaSessionBuilder;
use PommProject\Foundation\Session\Session;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
 * Pomm
 *
 * The Pomm service manager.
 *
 * @package Foundation
 * @copyright 2014 <NAME>
 * @author <NAME>
 * @license X11 {@link http://opensource.org/licenses/mit-license.php}
 */
class Pomm implements \ArrayAccess, LoggerAwareInterface
{
    protected $builders = [];
    protected $post_configurations = [];
    protected $sessions = [];
    protected $default;

    use LoggerAwareTrait;

    /**
     * __construct
     *
     * Instanciate a new Pomm Service class. It takes an array of
     * configurations as parameter. Following configurations settings are
     * supported by this service for each configuration:
     *
     * class_name   name of the DatabaseConfiguration class to instanciate.
     *
     * @access public
     * @param  array $configurations
     * @return void
     */
    public function __construct(array $configurations = [])
    {
        foreach ($configurations as $name => $configuration) {
            if (isset($configuration['class:session_builder'])) {
                $builder_class = $configuration['class:session_builder'];

                try {
                    $reflection = new \ReflectionClass($builder_class);

                    if (!$reflection->isSubClassOf('\PommProject\Foundation\Session\SessionBuilder')) {
                        throw new FoundationException(
                            sprintf(
                                "Class '%s' is not a subclass of \Pomm\Foundation\Session\SessionBuilder.",
                                $builder_class
                            )
                        );
                    }
                } catch (\ReflectionException $e) {
                    throw new FoundationException(
                        sprintf(
                            "Could not instanciate class '%s'.",
                            $builder_class
                        ),
                        null,
                        $e
                    );
                }
            } else {
                $builder_class = '\PommProject\Foundation\SessionBuilder';
            }

            $this->addBuilder($name, new $builder_class($configuration));

            if ($this->default === null || (isset($configuration['pomm:default']) && $configuration['pomm:default'] === true)) {
                $this->setDefaultBuilder($name);
            }
        }
    }

    /**
     * setDefaultBuilder
     *
     * Set the name for the default session builder.
     *
     * @access public
     * @param  string   $name
     * @return Pomm     $this
     */
    public function setDefaultBuilder($name)
    {
        if (!$this->hasBuilder($name)) {
            throw new FoundationException(
                sprintf(
                    "No such builder '%s'.",<commit_msg>Refactor Pomm service constructor code.<commit_after><?php
/*
 * This file is part of the PommProject/Foundation package.
 *
 * (c) 2014 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PommProject\Foundation;

use PommProject\Foundation\Exception\FoundationException;
use PommProject\Foundation\Session\SessionBuilder as VanillaSessionBuilder;
use PommProject\Foundation\Session\Session;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
 * Pomm
 *
 * The Pomm service manager.
 *
 * @package Foundation
 * @copyright 2014 <NAME>
 * @author <NAME>
 * @license X11 {@link http://opensource.org/licenses/mit-license.php}
 */
class Pomm implements \ArrayAccess, LoggerAwareInterface
{
    protected $builders = [];
    protected $post_configurations = [];
    protected $sessions = [];
    protected $default;

    use LoggerAwareTrait;

    /**
     * __construct
     *
     * Instanciate a new Pomm Service class. It takes an array of
     * configurations as parameter. Following configurations settings are
     * supported by this service for each configuration:
     *
     * class_name   name of the DatabaseConfiguration class to instanciate.
     *
     * @access public
     * @param  array $configurations
     * @return void
     */
    public function __construct(array $configurations = [])
    {
        foreach ($configurations as $name => $configuration) {
            if (isset($configuration['class:session_builder'])) {
                $builder_class = $this->checkSessionBuilderClass($configuration['class:session_builder']);
            } else {
                $builder_class = '\PommProject\Foundation\SessionBuilder';
            }

            $this->addBuilder($name, new $builder_class($configuration));

            if ($this->default === null || (isset($configuration['pomm:default']) && $configuration['pomm:default'] === true)) {
                $this->setDefaultBuilder($name);
            }
        }
    }

    /**
     * checkSessionBuilderClass
     *
     * Check if the given builder class is valid.
     *
     * @access private
     * @param  string               $builder_class
     * @throws FoundationException  if not valid
     * @return string               $builder_class
     */
    private function checkSessionBuilderClass($builder_class)
    {
        try {
            $reflection = new \ReflectionClass($builder_class);

            if (!$reflection->isSubClassOf('\PommProject\Foundation\Session\SessionBuilder')) {
                throw new FoundationException(
                    sprintf(
                        "Class '%s' is not a subclass of \Pomm\Foundation\Session\SessionBuilder.",
                        $builder_class
                    )
                );
            }
        } catch (\ReflectionException $e) {
            throw new FoundationException(
                sprintf(
                    "Could not instanciate class '%s'.",
                    $builder_class
                ),
                null,
                $e
            );
        }

        return $builder_class;
    }

    /**
     * setDefaultBuilder
     *
     * Set the name for the default session builder.
     *
     * @access public
     * @param  string   $name
     * @return Pomm     $this
     */
    public function setDefaultBuilder($name)
    {
        if (!$this->hasBuilder($name)) {
            throw new FoundationException(
                sprintf(
                    "No such builder '%s'.",
{'fraction_non_alphanumeric': 0.10047846889952153, 'fraction_numerical': 0.0028998115122517037, 'mean_word_length': 1.9142374313476975, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5246407', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1697, 'n_words': 446}