drillcoder /
AmoCRM_Wrap
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: DrillCoder |
||
| 5 | * Date: 11.09.17 |
||
| 6 | * Time: 16:07 |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace DrillCoder\AmoCRM_Wrap; |
||
| 10 | |||
| 11 | use stdClass; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class Lead |
||
| 15 | * @package DrillCoder\AmoCRM_Wrap |
||
| 16 | */ |
||
| 17 | class Lead extends BaseEntity |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | private $statusId; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | private $sale; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private $pipelineId; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | private $mainContactId; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param stdClass $data |
||
| 41 | * |
||
| 42 | * @return Lead |
||
| 43 | * |
||
| 44 | * @throws AmoWrapException |
||
| 45 | */ |
||
| 46 | public function loadInRaw($data) |
||
| 47 | { |
||
| 48 | BaseEntity::loadInRaw($data); |
||
| 49 | $this->sale = (int)$data->sale; |
||
| 50 | $this->pipelineId = (int)$data->pipeline->id; |
||
| 51 | $this->statusId = (int)$data->status_id; |
||
| 52 | if (isset($data->main_contact->id)) { |
||
| 53 | $this->mainContactId = (int)$data->main_contact->id; |
||
| 54 | } |
||
| 55 | |||
| 56 | return $this; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public function getSale() |
||
| 63 | { |
||
| 64 | return $this->sale; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param int $sale |
||
| 69 | * |
||
| 70 | * @return Lead |
||
| 71 | */ |
||
| 72 | public function setSale($sale) |
||
| 73 | { |
||
| 74 | $this->sale = $sale; |
||
| 75 | |||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function getStatusId() |
||
| 83 | { |
||
| 84 | return $this->statusId; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getStatusName() |
||
| 91 | { |
||
| 92 | $statuses = AmoCRM::getStatusesName($this->pipelineId); |
||
| 93 | |||
| 94 | return $statuses[$this->statusId]; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param int|string $pipelineIdOrName |
||
| 99 | * |
||
| 100 | * @return Lead |
||
| 101 | * |
||
| 102 | * @throws AmoWrapException |
||
| 103 | */ |
||
| 104 | public function setPipeline($pipelineIdOrName) |
||
| 105 | { |
||
| 106 | $this->pipelineId = AmoCRM::searchPipelineId($pipelineIdOrName); |
||
| 107 | |||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param int|string $statusIdOrName |
||
| 113 | * @param int|string $pipelineIdOrName |
||
| 114 | * |
||
| 115 | * @return Lead |
||
| 116 | * |
||
| 117 | * @throws AmoWrapException |
||
| 118 | */ |
||
| 119 | public function setStatus($statusIdOrName, $pipelineIdOrName = null) |
||
| 120 | { |
||
| 121 | $pipelineId = $pipelineIdOrName !== null ? AmoCRM::searchPipelineId($pipelineIdOrName) : $this->pipelineId; |
||
| 122 | $this->statusId = AmoCRM::searchStatusId($pipelineId, $statusIdOrName); |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return int |
||
| 129 | */ |
||
| 130 | public function getPipelineId() |
||
| 131 | { |
||
| 132 | return $this->pipelineId; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getPipelineName() |
||
| 139 | { |
||
| 140 | $pipelines = AmoCRM::getPipelinesName(); |
||
| 141 | |||
| 142 | return $pipelines[$this->pipelineId]; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | public function getMainContactId() |
||
| 149 | { |
||
| 150 | return $this->mainContactId; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return Contact |
||
| 155 | * |
||
| 156 | * @throws AmoWrapException |
||
| 157 | */ |
||
| 158 | public function getMainContact() |
||
| 159 | { |
||
| 160 | return new Contact($this->mainContactId); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param Contact|string|int $contact |
||
| 165 | * |
||
| 166 | * @return Lead |
||
| 167 | */ |
||
| 168 | public function setMainContact($contact) |
||
| 169 | { |
||
| 170 | $id = $contact instanceof Contact ? $contact->getId() : Base::onlyNumbers($contact); |
||
| 171 | $this->mainContactId = $id; |
||
|
0 ignored issues
–
show
|
|||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function isClosed() |
||
| 180 | { |
||
| 181 | return $this->statusId === 142 || $this->statusId === 143; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | protected function getExtraRaw() |
||
| 188 | { |
||
| 189 | return array( |
||
| 190 | 'pipeline_id' => $this->pipelineId, |
||
| 191 | 'sale' => $this->sale, |
||
| 192 | 'status_id' => $this->statusId, |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.