1<?php
2/**
3 * A Horde_Injector:: based Horde_Imsp_Auth:: factory.
4 *
5 * PHP version 5
6 *
7 * @category Horde
8 * @package  Core
9 * @author   Michael J Rubinsky <mrubinsk@horde.org>
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 */
12
13/**
14 * A Horde_Injector:: based Horde_Imsp_Auth:: factory.
15 *
16 * Copyright 2011-2017 Horde LLC (http://www.horde.org/)
17 *
18 * See the enclosed file COPYING for license information (LGPL). If you
19 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
20 *
21 * @category Horde
22 * @package  Core
23 * @author   Michael J Rubinsky <mrubinsk@horde.org>
24 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
25 */
26class Horde_Core_Factory_ImspAuth
27{
28    /**
29     * Instance cache
30     *
31     * @var array
32     */
33    protected static $_instances = array();
34
35    /**
36     *
37     * @var Horde_Injector
38     */
39    protected $_injector;
40
41    /**
42     * Constructor
43     *
44     * @param Horde_Injector $injector
45     */
46    public function __construct(Horde_Injector $injector)
47    {
48        $this->_injector = $injector;
49    }
50
51    /**
52     * Attempts to return a concrete Horde_Imsp_Auth instance based on $driver.
53     * Will only create a new object if one with the same parameters already
54     * does not exist.
55     *
56     * @param string $driver  Type of IMSP_Auth subclass to return.
57     * @param array $params   The driver parameters.
58     *
59     * @return Horde_Imsp_Auth
60     */
61    public static function create($driver, array $params = array())
62    {
63        //@TODO: Fix this.
64        /* Check for any imtest driver instances and kill them.
65           Otherwise, the socket will hang between requests from
66           seperate drivers (an Auth request and an Options request).*/
67        if (is_array(self::$_instances)) {
68            foreach (self::$_instances as $obj) {
69                if ($obj->getDriverType() == 'imtest') {
70                    $obj->logout();
71                }
72            }
73        }
74        $signature = serialize(array($driver, $params));
75        if (!isset(self::$_instances[$signature])) {
76            self::$_instances[$signature] = self::_factory($driver, $params);
77        }
78
79        return self::$_instances[$signature];
80    }
81
82    /**
83     * Attempts to return a concrete Horde_Imsp_Auth instance based on $driver
84     * Must be called as &Horde_Imsp_Auth::factory()
85     *
86     * @param  string $driver Type of Horde_Imsp_Auth subclass to return.
87     *
88     * @return mixed  The created Horde_Imsp_Auth subclass.
89     * @throws Horde_Exception
90     */
91    protected static function _factory($driver, array $params = array())
92    {
93        $driver = basename($driver);
94        $class = 'Horde_Imsp_Auth_' . $driver;
95
96        // Verify user/pass
97        if (empty($params['username'])) {
98            $params['username'] = $GLOBALS['registry']->getAuth('bare');
99            $params['password'] = $GLOBALS['registry']->getAuthCredential('password');
100        }
101
102        return new $class($params);
103    }
104
105}
106