quick minor path updates (#1968)
[openemr.git] / services / ProductRegistrationService.php
blob872728890c6ca5c2f87619a56f6bf611f341d130
1 <?php
2 /**
3 * ProductRegistrationService
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Matthew Vita <matthewvita48@gmail.com>
18 * @author Victor Kofia <victor.kofia@gmail.com>
19 * @link http://www.open-emr.org
22 namespace OpenEMR\Services;
24 use OpenEMR\Common\Database\Connector;
25 use OpenEMR\Common\Logging\Logger;
26 use OpenEMR\Entities\ProductRegistration;
28 require_once($GLOBALS['fileroot'] . "/interface/main/exceptions/invalid_email_exception.php");
29 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
30 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/duplicate_registration_exception.php");
32 class ProductRegistrationService
34 /**
35 * Logger used primarily for logging events that are of interest to
36 * developers.
38 private $logger;
40 /**
41 * The product registration repository to be used for db CRUD operations.
43 private $repository;
45 /**
46 * Default constructor.
48 public function __construct()
50 $this->logger = new Logger("\OpenEMR\Services\ProductRegistrationService");
51 $database = Connector::Instance();
52 $entityManager = $database->entityManager;
53 $this->repository = $entityManager->getRepository('\OpenEMR\Entities\ProductRegistration');
56 public function getProductStatus()
58 $this->logger->debug('Getting current product registration status');
59 $row = $this->repository->findFirst();
61 // Unboxing these here to avoid PHP 5.4 "Can't use method
62 // return value in write context" error.
63 $id = '';
64 $optOut = '';
66 if ($row !== null) {
67 $id = $row->getRegistrationId();
68 $optOut = $row->getOptOut();
71 if (empty($row)) {
72 $row = new ProductRegistration();
73 $row->setStatusAsString('UNREGISTERED');
74 } else if ($id !== 'null') {
75 $row->setStatusAsString('REGISTERED');
76 } else if (!empty($optOut) && $optOut == true) {
77 $row->setStatusAsString('OPT_OUT');
80 return $row;
83 public function registerProduct($email)
85 if (!$email || $email == 'false') {
86 $this->optOutStrategy();
87 return null;
88 } else {
89 return $this->optInStrategy($email);
93 private function optInStrategy($email)
95 $this->logger->debug('Attempting to register product with email ' . $email);
96 $curl = curl_init('https://reg.open-emr.org/api/registration');
97 curl_setopt($curl, CURLOPT_POST, true);
98 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email)));
99 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
100 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
101 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
102 $responseBodyRaw = curl_exec($curl);
103 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
104 curl_close($curl);
106 $this->logger->debug('Raw response from remote server: ' . $responseBodyRaw);
107 switch ($responseCode) {
108 case 201:
109 $responseBodyParsed = json_decode($responseBodyRaw);
111 $entry = new ProductRegistration();
112 $entry->setRegistrationId($responseBodyParsed->productId);
113 $entry->setEmail($email);
114 $entry->setOptOut(false);
116 $newId = $this->repository->save($entry);
117 $this->logger->debug('Successfully registered product ' . $newId);
119 return $newId;
120 break;
121 case 400:
122 throw new \InvalidEmailException($email . ' ' . xl("is not a valid email address"));
123 break;
124 case 409:
125 throw new \DuplicateRegistrationException(xl("Already registered"));
126 break;
127 default:
128 throw new \GenericProductRegistrationException(xl("Server error: try again later"));
132 // void... don't bother checking for success/failure.
133 private function optOutStrategy()
135 $this->logger->debug('Attempting to opt out of product registration');
136 $entry = new ProductRegistration();
137 $entry->setRegistrationId('null');
138 $entry->setEmail(null);
139 $entry->setOptOut(true);
141 $this->repository->save($entry);