Passed
Push — 1.0.0-dev ( 93958a...e1c8ef )
by nguereza
02:26
created

setDependencyInstanceFromParamOrCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 4
dl 0
loc 7
rs 10
1
<?php
2
	defined('ROOT_PATH') || exit('Access denied');
3
	/**
4
	 * TNH Framework
5
	 *
6
	 * A simple PHP framework using HMVC architecture
7
	 *
8
	 * This content is released under the GNU GPL License (GPL)
9
	 *
10
	 * Copyright (C) 2017 Tony NGUEREZA
11
	 *
12
	 * This program is free software; you can redistribute it and/or
13
	 * modify it under the terms of the GNU General Public License
14
	 * as published by the Free Software Foundation; either version 3
15
	 * of the License, or (at your option) any later version.
16
	 *
17
	 * This program is distributed in the hope that it will be useful,
18
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
	 * GNU General Public License for more details.
21
	 *
22
	 * You should have received a copy of the GNU General Public License
23
	 * along with this program; if not, write to the Free Software
24
	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
	*/
26
27
	class BaseClass{
28
		/**
29
		 * The logger instance
30
		 * @var object
31
		 */
32
		protected $logger;
33
34
		/**
35
		 * Class constructor
36
		 */
37
		public function __construct(){
38
			//Set Log instance to use
39
       		$this->setLoggerFromParamOrCreate(null);
40
		}
41
42
		/**
43
	     * Set the dependencies instance using argument or create new instance if is null
44
	     * @param string $name this class property name.
45
	     * @param object $instance the instance. If is not null will use it
46
	     * otherwise will create new instance.
47
	     * @param string $loadClassName the name of class to load using class_loader function.
48
	     * @param string $loadClassPath the path of class to load using class_loader function.
49
	     *
50
	     * @return object this current instance
51
	     */
52
	    protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){
53
	      if ($instance !== null){
54
	        $this->{$name} = $instance;
55
	        return $this;
56
	      }
57
	      $this->{$name} =& class_loader($loadClassName, $loadClassePath);
58
	      return $this;
59
	    }
60
61
	    /**
62
	     * Return the Log instance
63
	     * @return object
64
	     */
65
	    public function getLogger(){
66
	      return $this->logger;
67
	    }
68
69
	    /**
70
	     * Set the log instance
71
	     * @param object $logger the log object
72
		 * @return object Database
73
	     */
74
	    public function setLogger($logger){
75
	      $this->logger = $logger;
76
	      return $this;
77
	    }
78
79
	    /**
80
	     * Set the Log instance using argument or create new instance
81
	     * @param object $logger the Log instance if not null
82
	     *
83
	     * @return object this current instance
84
	     */
85
	    protected function setLoggerFromParamOrCreate(Log $logger = null){
86
	      $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes');
87
	      if ($logger === null){
88
	        $this->logger->setLogger('Class::' . get_class($this));
89
	      }
90
	      return $this;
91
	    }
92
93
	}
94