<commit_before><?php

namespace ZfcUser\Authentication\Adapter;

use Zend\Authentication\Adapter\AdapterInterface,
    Zend\Authentication\Result as AuthenticationResult,
    Zend\EventManager\Event,
    Zend\Stdlib\RequestDescription as Request,
    Zend\Stdlib\ResponseDescription as Response,
    ZfcBase\EventManager\EventProvider,
    Zend\EventManager\EventCollection;

class AdapterChain extends EventProvider implements AdapterInterface
{
    /**
     * @var AdapterChainEvent
     */
    protected $event;

    /**
     * @var ChainableAdapter
     */
    protected $defaultAdapter;

    /**
     * Returns the authentication result 
     * 
     * @return AuthenticationResult
     */
    public function authenticate()
    {
        $e = $this->getEvent();

        $result = new AuthenticationResult(
            $e->getCode(),
            $e->getIdentity(),
            $e->getMessages()
        );

        $this->resetAdapters();

        return $result;
    }

    public function prepareForAuthentication(Request $request)
    {
        $e = $this->getEvent()
                  ->setRequest($request);

        $this->events()->trigger('authenticate.pre', $e);

        $result = $this->events()->trigger('authenticate', $e, function($test) {
            return ($test instanceof Response);
        });

        if ($result->stopped()) {
            if($result->last() instanceof Response) {
                return $result->last();
            } else {
                // throw new Exception('Auth event was stopped without a response.');
            }
        }

        if ($e->getIdentity()) {
            return true;
        }

        return false;
    }

    /**
     * Attach a chainable adapter 
     * 
     * @param ChainableAdapter $defaultAdapter 
     * @return AdapterChain
     */
    public function setDefaultAdapter(ChainableAdapter $defaultAdapter)
    {
        $this->defaultAdapter = $defaultAdapter;
        return $this;
    }

    /**
     * Set the event manager instance used by this context
     * 
     * @param  EventCollection $events 
     * @return mixed
     */
    public function setEventManager(EventCollection $events)
    {
        $return = parent::setEventManager($events);
        if ($this->defaultAdapter) {
            $this->attach($this->defaultAdapter);
        }
        return $return;
    }

    /**
     * attach 
     * 
     * @param ChainableAdapter $adapter 
     * @return AdapterChain
     */
    public function attach(ChainableAdapter $adapter)
    {
        //$adapter->getStorage()->clear();
        $this->events()->attach('authenticate', array($adapter, 'authenticate'));
        return $this;
    }

    /**
     * resetAdapters 
     * 
     * @return AdapterChain
     */
    public function resetAdapters()
    {
        $listeners = $this->events()->getListeners('authenticate');
        foreach ($listeners as $listener) {
            $listener = $listener->getCallback();
            if (is_array($listener) && $listener[0] instanceof ChainableAdapter) {
                $listener[0]->getStorage()->clear();
            }
        }
        $this;
    }

    /**
     * Get the auth event 
     * 
     * @return AdapterChainEvent
     */
    public function getEvent()
    {
        if (null === $this->event) {
            $this->setEvent(new AdapterChainEvent);
            $this->event->setTarget($this);
        }
        return $this->event;
    }

    /**
     * Set an event to use during dispatch
     *
     * By default, will re-cast to AdapterChainEvent if another event type is provided.
     * 
     * @param  Event $e 
     * @return AdapterChain
     */
    public function setEvent(Event $e)
    {
        if ($e instanceof Event && !$e instanceof AdapterChainEvent) {
            $eventParams = $e->getParams();
            $e = new AdapterChainEvent();
            $e->setParams($eventParams);
            unset($eventParams);
        }
        $this->event = $e;
        return $this;
    }
} 
<commit_msg>Less fragile method of attaching default auth adapter.<commit_after><?php

namespace ZfcUser\Authentication\Adapter;

use Zend\Authentication\Adapter\AdapterInterface,
    Zend\Authentication\Result as AuthenticationResult,
    Zend\EventManager\Event,
    Zend\Stdlib\RequestDescription as Request,
    Zend\Stdlib\ResponseDescription as Response,
    ZfcBase\EventManager\EventProvider,
    Zend\EventManager\EventCollection;

class AdapterChain extends EventProvider implements AdapterInterface
{
    /**
     * @var AdapterChainEvent
     */
    protected $event;

    /**
     * @var ChainableAdapter
     */
    protected $defaultAdapter;

    /**
     * Returns the authentication result 
     * 
     * @return AuthenticationResult
     */
    public function authenticate()
    {
        $e = $this->getEvent();

        $result = new AuthenticationResult(
            $e->getCode(),
            $e->getIdentity(),
            $e->getMessages()
        );

        $this->resetAdapters();

        return $result;
    }

    public function prepareForAuthentication(Request $request)
    {
        $e = $this->getEvent()
                  ->setRequest($request);

        $this->events()->trigger('authenticate.pre', $e);

        $result = $this->events()->trigger('authenticate', $e, function($test) {
            return ($test instanceof Response);
        });

        if ($result->stopped()) {
            if($result->last() instanceof Response) {
                return $result->last();
            } else {
                // throw new Exception('Auth event was stopped without a response.');
            }
        }

        if ($e->getIdentity()) {
            return true;
        }

        return false;
    }

    /**
     * Attach a chainable adapter 
     * 
     * @param ChainableAdapter $defaultAdapter 
     * @return AdapterChain
     */
    public function setDefaultAdapter(ChainableAdapter $defaultAdapter)
    {
        $this->defaultAdapter = $defaultAdapter;
        $this->attachDefaultAdapter();
        return $this;
    }

    /**
     * Set the event manager instance used by this context
     * 
     * @param  EventCollection $events 
     * @return mixed
     */
    public function setEventManager(EventCollection $events)
    {
        $return = parent::setEventManager($events);
        $this->attachDefaultAdapter();
        return $return;
    }

    /**
     * attach 
     * 
     * @return AdapterChain
     */
    public function attachDefaultAdapter()
    {
        //$adapter->getStorage()->clear();
        if (!$this->defaultAdapter || !$this->events) {
            return;
        }
        $this->events()->attach('authenticate', array($this->defaultAdapter, 'authenticate'));
        return $this;
    }

    /**
     * resetAdapters 
     * 
     * @return AdapterChain
     */
    public function resetAdapters()
    {
        $listeners = $this->events()->getListeners('authenticate');
        foreach ($listeners as $listener) {
            $listener = $listener->getCallback();
            if (is_array($listener) && $listener[0] instanceof ChainableAdapter) {
                $listener[0]->getStorage()->clear();
            }
        }
        $this;
    }

    /**
     * Get the auth event 
     * 
     * @return AdapterChainEvent
     */
    public function getEvent()
    {
        if (null === $this->event) {
            $this->setEvent(new AdapterChainEvent);
            $this->event->setTarget($this);
        }
        return $this->event;
    }

    /**
     * Set an event to use during dispatch
     *
     * By default, will re-cast to AdapterChainEvent if another event type is provided.
     * 
     * @param  Event $e 
     * @return AdapterChain
     */
    public function setEvent(Event $e)
    {
        if ($e instanceof Event && !$e instanceof AdapterChainEvent) {
            $eventParams = $e->getParams();
            $e = new AdapterChainEvent();
            $e->setParams($eventParams);
            unset($eventParams);
        }
        $this->event = $e;
        return $this;
    }
} 

{'fraction_non_alphanumeric': 0.12350253180190193, 'fraction_numerical': 0.0004940101272076078, 'mean_word_length': 1.9554744525547445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '3743940', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2136, 'n_words': 472}