Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.76% covered (warning)
75.76%
25 / 33
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageFactory
75.76% covered (warning)
75.76%
25 / 33
0.00% covered (danger)
0.00%
0 / 1
10.15
0.00% covered (danger)
0.00%
0 / 1
 __invoke
75.76% covered (warning)
75.76%
25 / 33
0.00% covered (danger)
0.00%
0 / 1
10.15
1<?php
2
3/**
4 * Factory for Image CAPTCHA module.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  CAPTCHA
25 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Captcha;
31
32use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
33use Laminas\ServiceManager\Exception\ServiceNotFoundException;
34use Laminas\ServiceManager\Factory\FactoryInterface;
35use Psr\Container\ContainerExceptionInterface as ContainerException;
36use Psr\Container\ContainerInterface;
37
38use function is_callable;
39
40/**
41 * Image CAPTCHA factory.
42 *
43 * @category VuFind
44 * @package  CAPTCHA
45 * @author   Mario Trojan <mario.trojan@uni-tuebingen.de>
46 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org/wiki/development Wiki
48 */
49class ImageFactory implements FactoryInterface
50{
51    /**
52     * Create an object
53     *
54     * @param ContainerInterface $container     Service manager
55     * @param string             $requestedName Service being created
56     * @param null|array         $options       Extra options (optional)
57     *
58     * @return object
59     *
60     * @throws ServiceNotFoundException if unable to resolve the service.
61     * @throws ServiceNotCreatedException if an exception is raised when
62     * creating a service.
63     * @throws ContainerException&\Throwable if any other error occurs
64     */
65    public function __invoke(
66        ContainerInterface $container,
67        $requestedName,
68        array $options = null
69    ) {
70        if (!empty($options)) {
71            throw new \Exception('Unexpected options passed to factory.');
72        }
73
74        $cacheManager = $container->get(\VuFind\Cache\Manager::class);
75        $cacheOptions = $cacheManager->getCache('public')->getOptions();
76        if (!is_callable([$cacheOptions, 'getCacheDir'])) {
77            throw new \Exception('Image CAPTCHA requires access to public cache; is cache disabled?');
78        }
79        $imageOptions = [
80            'font' => APPLICATION_PATH
81                    . '/vendor/webfontkit/open-sans/fonts/opensans-regular.ttf',
82            'imgDir' => $cacheOptions->getCacheDir(),
83        ];
84
85        $config = $container->get(\VuFind\Config\PluginManager::class)
86            ->get('config');
87
88        if (isset($config->Captcha->image_length)) {
89            $imageOptions['wordLen'] = $config->Captcha->image_length;
90        }
91        if (isset($config->Captcha->image_width)) {
92            $imageOptions['width'] = $config->Captcha->image_width;
93        }
94        if (isset($config->Captcha->image_height)) {
95            $imageOptions['height'] = $config->Captcha->image_height;
96        }
97        if (isset($config->Captcha->image_fontSize)) {
98            $imageOptions['fsize'] = $config->Captcha->image_fontSize;
99        }
100        if (isset($config->Captcha->image_dotNoiseLevel)) {
101            $imageOptions['dotNoiseLevel'] = $config->Captcha->image_dotNoiseLevel;
102        }
103        if (isset($config->Captcha->image_lineNoiseLevel)) {
104            $imageOptions['lineNoiseLevel'] = $config->Captcha->image_lineNoiseLevel;
105        }
106
107        $baseUrl = rtrim(
108            ($container->get('ViewHelperManager')->get('url'))('home') ?? '',
109            '/'
110        );
111        return new $requestedName(
112            new \Laminas\Captcha\Image($imageOptions),
113            "$baseUrl/cache/"
114        );
115    }
116}