1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mothership
22: * @package Mothership_Exception
23: * @author Maurizio Brioschi <brioschi@mothership.de>
24: * @copyright Copyright (c) 2015 Mothership GmbH
25: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26: * @link http://www.mothership.de/
27: */
28:
29: namespace Mothership\Exception;
30:
31: use Exception;
32: use Symfony\Component\Console\Output\ConsoleOutput;
33: use Symfony\Component\Console\Output\OutputInterface;
34:
35: abstract class ExceptionAbstract extends Exception
36: {
37: protected $gravity; //score from 0 to 100 where 100 is the most dangerous
38: protected $output;
39:
40: /**
41: * @param string $message
42: * @param int $code
43: * @param Exception|null $previous
44: * @param OutputInterface|null $output
45: * @param bool|true $send_alert if is true the exception will be write on the $output
46: */
47: public function __construct($message = "", $code = 0, Exception $previous = null, OutputInterface $output = null,
48: $send_alert = true)
49: {
50: parent::__construct($message, $code, $previous);
51: if ($previous != null) {
52: $this->message .= "\n" . $previous->getMessage();
53: }
54:
55: $this->output = $output;
56: if (is_null($output) || !isset($output)) {
57: $this->output = new ConsoleOutput();
58: }
59:
60: $this->gravity = $this->code;
61:
62: if ($send_alert && $previous == null) {
63: $this->alert();
64: }
65: }
66:
67: /**
68: * Get the gravity of the exception
69: * @return int
70: */
71: public function getGravity()
72: {
73: return $this->gravity;
74: }
75:
76: /**
77: * Get the gravity level of the exception
78: * @return string
79: */
80: protected function getGravityLevel()
81: {
82: switch ($this->gravity) {
83: case $this->gravity > 90:
84: return "danger";
85: case $this->gravity >= 80 && $this->gravity < 90:
86: return "low-danger";
87: case $this->gravity >= 50 && $this->gravity < 80:
88: return "warning";
89: default:
90: return "info";
91: }
92: }
93:
94: public function alert()
95: {
96: $level = $this->getGravityLevel();
97: switch ($level) {
98: case 'danger':
99: $this->output->writeln("<error>DANGER: " . $this->message . "\n\nTHIS IS THE END!!!</error>");
100: break;
101: case 'low-danger':
102: $this->output->writeln("<error>DANGER: " . $this->message . "</error>");
103: break;
104: case 'waring':
105: $this->output->writeln("<comment>WARNING: " . $this->message . "</comment>");
106: break;
107: case 'info':
108: $this->output->writeln("<info>INFO: " . $this->message . "</info>");
109: break;
110: }
111:
112: }
113: }
114:
115: