Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Budget
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 charge
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Pdf\Extract\Filter;
16
17use Pop\Pdf\Extract\Exception;
18
19/**
20 * Pdf extract filter decode budget class
21 *
22 * @category   Pop
23 * @package    Pop\Pdf
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    6.0.0
28 */
29class Budget
30{
31
32    /**
33     * Remaining bytes available before the budget is exhausted
34     * @var int
35     */
36    protected int $remaining;
37
38    /**
39     * Constructor
40     *
41     * Instantiate a decode budget with a total byte allowance.
42     *
43     * @param int $totalBytes
44     */
45    public function __construct(int $totalBytes)
46    {
47        $this->remaining = $totalBytes;
48    }
49
50    /**
51     * Charge a number of bytes against the remaining budget
52     *
53     * @param  int $bytes
54     * @throws Exception
55     * @return void
56     */
57    public function charge(int $bytes): void
58    {
59        $this->remaining -= $bytes;
60
61        if ($this->remaining < 0) {
62            throw new Exception('Error: Exceeded the maximum total decoded size for this document.');
63        }
64    }
65
66}