Completed
Pull Request — develop (#178)
by
unknown
09:34
created

GeometryCalculator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 74
ccs 38
cts 42
cp 0.9048
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C calculateOffsets() 0 26 8
C calculateExtents() 0 29 8
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation;
19
20
/**
21
 * PhpOffice\PhpPresentation\GeometryCalculator
22
 */
23
class GeometryCalculator
24
{
25
    const X = 'X';
26
    const Y = 'Y';
27
28
    /**
29
    * Calculate X and Y offsets for a set of shapes within a container such as a slide or group.
30
    *
31
    * @param  \PhpOffice\PhpPresentation\ShapeContainerInterface $container
32
    * @return array
33
    */
34 67
    public static function calculateOffsets(ShapeContainerInterface $container)
35
    {
36 67
        $offsets = array(self::X => 0, self::Y => 0);
37
38 67
        if ($container !== null && count($container->getShapeCollection()) != 0) {
39 58
            $shapes = $container->getShapeCollection();
40 58
            if ($shapes[0] !== null) {
41 58
                $offsets[self::X] = $shapes[0]->getOffsetX();
42 58
                $offsets[self::Y] = $shapes[0]->getOffsetY();
43 58
            }
44
45 58
            foreach ($shapes as $shape) {
46 58
                if ($shape !== null) {
47 58
                    if ($shape->getOffsetX() < $offsets[self::X]) {
48
                        $offsets[self::X] = $shape->getOffsetX();
49
                    }
50
51 58
                    if ($shape->getOffsetY() < $offsets[self::Y]) {
52
                        $offsets[self::Y] = $shape->getOffsetY();
53
                    }
54 58
                }
55 58
            }
56 58
        }
57
58 67
        return $offsets;
59
    }
60
61
    /**
62
    * Calculate X and Y extents for a set of shapes within a container such as a slide or group.
63
    *
64
    * @param  \PhpOffice\PhpPresentation\ShapeContainerInterface $container
65
    * @return array
66
    */
67 67
    public static function calculateExtents(ShapeContainerInterface $container)
68
    {
69 67
        $extents = array(self::X => 0, self::Y => 0);
70
71 67
        if ($container !== null && count($container->getShapeCollection()) != 0) {
72 58
            $shapes = $container->getShapeCollection();
73 58
            if ($shapes[0] !== null) {
74 58
                $extents[self::X] = $shapes[0]->getOffsetX() + $shapes[0]->getWidth();
75 58
                $extents[self::Y] = $shapes[0]->getOffsetY() + $shapes[0]->getHeight();
76 58
            }
77
78 58
            foreach ($shapes as $shape) {
79 58
                if ($shape !== null) {
80 58
                    $extentX = $shape->getOffsetX() + $shape->getWidth();
81 58
                    $extentY = $shape->getOffsetY() + $shape->getHeight();
82
83 58
                    if ($extentX > $extents[self::X]) {
84 2
                        $extents[self::X] = $extentX;
85 2
                    }
86
87 58
                    if ($extentY > $extents[self::Y]) {
88 2
                        $extents[self::Y] = $extentY;
89 2
                    }
90 58
                }
91 58
            }
92 58
        }
93
94 67
        return $extents;
95
    }
96
}
97